1.3. Anatomy of the C source code

Now, let us break and dive deeper into the anatomy of file hello_world.c that we used in Basic Compilation.

hello_world.c — Header Include
1
2
3
4
5
6
#include <stdio.h>

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

The line no. 1 we are including a header file. stdio.h in this case. stdio.h is the file that will help us to do Standard Input Output. This file is part of the standard C library.

In a C program whenever you are going to use functions which are not implemented by you, it is necessary to include a header file that gives us their declaration and function signature. This is also required to be done, whether the function is implemented by an in-house library or standard C library.

In line number 4 we are calling one such function, printf. This function is part of the C standard library. printf is not a C programming language keyword in itself. As far as printf is concerned, for C language, it is just another function. To be fair and square, printf is as good a function as any another function in your complete software stack. The only thing different about this function is that it is going to come from a standard library and not from your internal implementation.

hello_world.c — main() — The entry point
1
2
3
4
5
6
#include <stdio.h>

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

At line no. 3, we are implementing/defining the function main(). The entry point of each and every C program is main(). In this case we are defining it as int main(). This program does not take any parameter but returns an integer. This function is defined between matching curly braces between line no. 3 and line no. 6

hello_world.c — printf — The functional code
1
2
3
4
5
6
#include <stdio.h>

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

At line no. 4, we are actually printing Hello World!. The n is for the new line. printf is the function that is responsible to print data on the screen.

hello_world.c — return success
1
2
3
4
5
6
#include <stdio.h>

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

When we invoked / executed a.out in this case, after the a.out finished its execution, it returned an integer status code back to the operating system / script / shell that executed a.out. return 0; is responsible to return this value back. By convention, 0 is marked as success status of any process and anything non-zero is marked and interpreted as failure. So, at line no. 5, we return 0.