This is a simple program to print “Hello world”
#include <stdio.h> int main() { // Single line comment /* Multiline comment */ printf("\n Hello world "); return 0; }
Components of the C program:
Preprocessor:
#include
is the first word of any C program. It is known as a pre-processor. The task of a pre-processor is to initialize the environment of the program, i.e. to link the program with the header file required.
This is the first stage of compilation where our source is expanded by the inclusion of another file called stdio.h
There are many types of preprocessor commands, and one among them is #include which helps us to include content from other sources in our source code.
Header file:
A header file is a collection of built-in functions along with its definition, which can directly be used in our program. Any header file is incorporated into any C program by using pre-pre-processor #include statement with the header file.
Standard header files are provided with each compiler and cover a wide range of areas like string handling, mathematical functions, data conversion, printing, and reading of variables.
stdio stands for standard input and output and it has function definition for scanf() and printf() to take the input and display the output respectively.
Note:
Any file with the name as <name_of_file>.h is called a header file. So the purpose of any header file is to have such variables, function declaration, function definition which need to be shared among many source files.
Thus, if we see the use of any variable or function in many source files, we just have them inside the header file and include those in our source file.
This stdio is the source file, often referred to as the C library, where many of the C built-in functions like print and scan are defined. Thus C library is a collection of built-in functions.
A few of the other important C libraries are: string.h, stdlib.h.
The main function
Every C program must have this function and it is the starting point of any programs.
int main() { ... }
In the above section int written before the main is the return type of the function So here we wrote a function which would return some value whose type must be integer. The curly braces { } just after the main function encloses the body of the main() function.
Arguments passed will be discussed in later programs.
The statement or instructions
So inside the braces we would be writing our logic which does our jobs. In this program our objective is just to print “Hello world”. Thus we used the function which prints on the console and that function is “printf” which is included from a library called stdio.h.
Note:
- Every statement in C ends with colon(;) which marks the end of the statement.
- In printf, “f” stands for print formatted output. To be discussed later on this.
The return statement
A return statement ends the execution and if its the function, and returns control to the calling function. Execution resumes in the calling function at this point immediately following the call. It can return value to the calling function.
In this function, the return value of 0 means successful execution of the main function, and the non-zero value indicates some error and the program does not execute successfully.
Comments:
We can add comments in a program to describe the purpose of the program. These comments are ignored by the compiler and are not executed.
To add a single line comment, start by adding two forward slashes // followed by comment and to have multiline comment, enclose it between /* ….. and */, as in the above program.

Now we can expect two important points to be thought over:
1) Why int main () not void main () as our objective was just to print “hello world” and not to return anything ?
2) Why only return 0 , why not 1 or any other digits?
The answer to both these questions is related to the Unix operating system. In general, any function in the Unix operating system returns 0 on success and non-zero on failure. So just to convey this information about the successful execution of the program to the operating system, we return 0, and we know the function has a syntax < return-type> <name> <argument>, So we use ‘int’ as the return type, not void as a void means a function does not return anything.
We could return non-zero but ideally as good programming practice we should not.
The above points and explanation to the main program is very important for C interview Questions.
Relevant posts:
- Pre-processor directives
- Header file
- Functions
- Variables
Categories: C Language
Leave a Reply