CC · Lesson 1 of 8

Hello, World!

The original Hello World was written in C in 1974 by Brian Kernighan. We're still writing it today. This is either a testament to C's longevity or our lack of creativity.

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

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 error. 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;
}
◆ Note
The return 0; at the end of main() tells the operating system the program ran successfully. You can omit it in C99 and later, but it's good practice to be explicit.