BeginnerLesson 1 of 18
Hello World, main & the Compiler – C
Your first C program, its parts and the path from source code to an executable file.
A C program consists of functions. Execution always starts in the function main. You print text with printf (from <stdio.h>).
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}#include <stdio.h>includes the declarations for standard input/output (includingprintf).int main(void)is the entry point;voidmeans: no parameters.\nis the newline –printfdoes not append it automatically.return 0;reports "success" to the operating system; anything other than 0 means an error.- Every statement ends with a semicolon
;, and blocks are enclosed in{ }.
C is a compiled language. The path from source code to program has four stages, which gcc normally performs in a single step:
gcc -E hallo.c -o hallo.i # 1. Preprocessor: resolve #include and #define
gcc -S hallo.c # 2. Compiler: C -> assembly (hallo.s)
gcc -c hallo.c # 3. Assembler: -> object file (hallo.o)
gcc hallo.o -o hallo # 4. Linker: object files + libraries -> programYou can see comments and escape sequences (\n newline, \t tab, \" double quote, \\ backslash) here:
#include <stdio.h>
int main(void) {
/* Block comment spanning
several lines */
printf("Line 1\n"); // Line comment (since C99)
printf("Tab:\tText\n");
printf("Quote: \"Hi\" and backslash: \\\n");
return 0;
}TipAlways read compiler error messages starting from the first line – follow-up errors often stem from a single forgotten semicolon or a missing bracket further up.
WarningWithout
#include <stdio.h>, the compiler does not know printf. Modern compilers then abort or warn loudly – never ignore such warnings.