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. C is a pretty old programming language. It has been there since around 1972. However it does not mean every one know it. Since 1972 there have been many programming language. Some based on C or C++ or some totally different. On the same grounds, may be every software programmer also does not know about C language. This book tries to do justice to such programmers who do not have decent exposure to C programming language and hence tries to explain about C programming in little bit more details which may bore the developers who already know C programming language. Apologies for that.

hello_world.c — Header Include
1#include <stdio.h>
2
3int main() {
4    printf ("Hello World!\n");
5    return 0;
6}

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#include <stdio.h>
2
3int main() {
4    printf ("Hello World!\n");
5    return 0;
6}

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/executable does not take any parameter but returns an integer status. This function is defined between matching curly braces between line no. 3 and line no. 6

hello_world.c — printf — The functional code
1#include <stdio.h>
2
3int main() {
4    printf ("Hello World!\n");
5    return 0;
6}

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#include <stdio.h>
2
3int main() {
4    printf ("Hello World!\n");
5    return 0;
6}

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.