This post is a bit of extra reading to accompany the “Hello World!” tutorial. We’ll be returning to the original code and explaining how it works and what each line does.
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
Let’s start with the first line: #include <stdio.h>. This line defines what’s called preprocessing directive- which basically works with the compiler to execute your code. stdio.h is called a header, and is one of several standard headers used in C. The stdio.h header contains declarations for standard input and output functions, one of which is the printf function.
On the next line, we’re declaring a function called “main”. But this isn’t just some arbitrary name we’re giving the function- it’s the function that C looks for in order to begin execution of the program. int stands for integer, and indicates that the function will return an integer- in this case, zero. The curly braces surround the contents of the function, indicating its boundaries.
printf (which was declared in stdio.h), which stands for “print formatted”, parses an input argument and outputs the result to the screen. The argument is the string that we type between the double quotes. As you already know, \n (an escape sequence) simply means a newline. printf is actually an int function, and therefore returns an integer- but this is discarded so you don’t need to worry about it. The semicolon terminates the statement.
As we’ve already discussed, main is an int function, and must therefore return an integer. This is what return 0; does. C interprets zero as a successful execution.
And that’s it!