C programming basics

Hello, World

The following code is just a basic Hello, World program where text is printed to screen.

#include <stdio.h>
int main(void) {
    printf("Hello, World\n");
    return 0;
}

Going line by line:

  • #include <stdio.h>– this includes a standard IO header file from your system in your program, so the printf function works. In C programs there are many standard library header files used from the system and they come with the compiler, so you don’t need to reinvent everything and you have best practices at your disposal.
  • int main(void) – each C program starts with a main() function. The intkeyword indicates that main returns a value of integer type. The voidkeyword indicates that function doesn’t take any arguments.
  • { and } – curly brackets wrap a block of code such as main function.
  • printf("Hello, World\n"); – this prints text Hello, World text and a new line on your screen when the program is compiled and run. The additional \n stands for a new line character.
  • return 0; – the main function returns a value 0.

Compiling

In order to run your Hello, World program, you need to first compile it.

To compile a C program, you need to have a compiler. In the above case, we will use agcc compiler.

gcc hello.c -o hello.o
  • gcc is a command for running a compiler
  • hello.c is a C source code file with the above code
  • -o hello.o is optional and defines a binary file that is created. If left out the output file will be automatically set by the compiler. For example, a.out.

You can then run this C program by executing

./hello.o

Which prints in the command line:

Hello, World

Arguments and return values

You can pass arguments to main():

#include <stdio.h>

int main(int argc, char **argv) {
    printf("Hello, %s\n", argv[1]);
    return 0;
}
  • int main(...) – this means that the main() function will return a value of integer type.
  • int argc – the first argument defines the number of arguments that will be passed the compiled program.
  • char **argv – the second argument defines an array of character strings.

Let’s compile and run the program:

gcc hello.c -o hello.o
./hello.o "PHP"

The output of the above program will be:

Hello, PHP

Code comments

In C there are two types of comments:

  • Single line comments
#include <stdio.h>

// This is a single line comment.
int main(void) {
    printf("Hello, World\n");
    return 0;
}
  • Multi-line comments

They are wrapped between /* and */:

#include <stdio.h>

/*
 * This is a multi-line comment.
 */
int main(void) {
    printf("Hello, World\n");
    return 0;
}

Function printf()

The above has already been used the printf() function.

Arrays, loops, and break

An array can be imagined as a container with a fixed number of elements, where each element is indexed from 0 incrementally:

| Array element | H | e | l | l | o || Array index | 0 | 1 | 2 | 3 | 4 |

Let’s check a code example:

#include <stdio.h>

char *strings[5];

int main() {
    int i;

    strings[0] = "H";
    strings[1] = "e";
    strings[2] = "l";
    strings[3] = "l";
    strings[4] = "o";

    for (i = 0; i < 5; i++) {
        printf("%s\n", strings[i]);
    }
}

Leave a Reply

Your email address will not be published.