Boiler Plate code:

helloworld.c
#include <stdio.h>

int main() {
  /* write your code here */
  return 0;
}

Note: This is the basic code, you have to write everytime while writing C program.


Now we will understand the example from the previous chapter.

Understanding the pervious example:

helloworld.c
#include <stdio.h>

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

Components:

Basically, there are three components of C code there:
  1. Header Files
  2. Main Function
  3. Return Statement

Note : This partitioning has no technical basis. This is done just for the purpose for understanding the code easily for new comers.


Code Description:

  • Line1 : stdio.h is a header file in C which provides functionality for input and output and many other functions. It is included in the header file section.
  • Line2 : int main() this is the starting point for the execution of your program. Any code which is written inside its curly brackets will be executed. It is included in the main function section.
  • Line3 : printf("Hello World!") is printing "Hello World!" on screen. It is included in the main function section.
  • Line4 : return 0 means that we are returning from main function there. This is the last line of the main function which is executed. Everything written after it is not executed. It is the exit point of our main function. It is the return statement.

What does return 0 mean:

Whenever we write a function in C language, it returns some value. The type of value is the return type of the function. In our case, return type of our function is int as you can see. That is why it is returning 0. If our function has return type void, it will not return anything.

Some important points regarding syntax of C:

  • You need to put a semicolon (;) after every line of code.
  • C compiler ignores white spaces. You can write all code in one line but this is not advisable.
  • C is case sensitive language. E.g. Sam is not same as sam.
  • If you have any confusion, you can read this tutorial again.