Hello World!

Everyone's first program is Hello World!

Hello World!

Let us write the most basic program to illustrate the language that is C, a "Hello World!" program that outputs simply: Hello world!.

//main.c

#include <stdio.h>

int main() {
    // This is a comment.
    
    /* This is a multi-lined comment.
     * These gray lines are ignored by the program.
     * It is only for the developer, to remember something,
     * or to tell their peers how the program works.
     */
    
    printf("Hello, World!\n"); // \n means newline
    return 0;
    
    // Console:
    // Hello, World!
    // 
}

The program can be dissected as follows:

  • #include <stdio.h> This line tells the program we are using features within a library named stdio.h, printf() to be exact, as it is defined within the library.

  • int main() {} This is a function definition. It is the entry point of the program, that means, the program will start right inside it. main() must exist in every C program. We write the main code inside this function.

  • printf("Hello, World!\n"); This is a function call, like using a mathematics function, we pass parameters within the parenthesis. This function can output whatever parameter we put into the console, so we want it to write out Hello, World!. The function is defined within the library we have included earlier. '\n' represents a "newline" character, since we cannot actually store an Enter key in a single line.

  • return 0; When we run a C program, it ends by giving us an exit code to indicate whether or not there is a problem. By returning 0, we indicate that the program ended successfully.

    The main() function is a specific case, in that you can only omit the return statement. The compiler will automatically add them return 0; for you. However, for other functions, you must return something, unless the return type is void.

Some things to take note of:

  • C is case-sensitive. Upper-case letters and lower-case letters are entirely different! The following will generate an error:

  • End each statement with a ; like how you would end a sentence with a full stop in English. Here is an example of an error related to this:

  • Close as many { or ( or " as there are with } or ) or " and do not mess up the order. It is just like maths, or else, the following error will be generated:

Last updated