Lesson 1 of 12

Hello, World!

Write, compile and run your first C program, and see the basic shape every C program shares.

C programs start with #include directives for headers, followed by . The main() function is the entry point. printf() from <stdio.h> prints formatted output to the .

C
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Compile with gcc hello.c -o hello, then run with ./hello. The -o flag specifies the output filename. main() returns an int — 0 means success, non-zero means . The \n is a newline escape sequence; printf() doesn't add one automatically.

C
#include <stdio.h>

int main() {
    // printf uses format specifiers
    printf("Hello, World!\n");
    printf("The answer is %d\n", 42);
    printf("Pi is approximately %.4f\n", 3.14159);
    printf("Name: %s\n", "Alice");
    printf("Char: %c\n", 'A');

    // printf returns the number of characters written
    int chars = printf("Count me\n");
    printf("That was %d characters\n", chars);

    return 0;
}
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.