Skip to content
Home » Programming » C Examples » Hello World Program in C with explanation

Hello World Program in C with explanation

Hello World Program in C

In this post, we will be taking a look in Hello World Program in C. In this program, we will print “Hello World!” and explain the syntax of the program.

Program to print Hello World in C

// Preprocessor Directive
#include<stdio.h>

// declaration of main function
int main() {
     // printf() displays string in the display
     printf("Hello World!");
     return 0;
}

Output

Hello World!

Explanation:

  • In the first line, we can see the sentence followed by two forward slashes “//“. These are called comments.
// Preprocessor Directive
. . .
// declaration of main function
. . .
// printf() displays string in the display
. . .

The comments are just for decorations. That means, the text written in comments does not affect the overall program. Basically, comments are used to explain what is the use of certain block of program.

  • After the first comment, we have preprocessor directive.
#include<stdio.h>

Here, #include is the preprocessor directive. This is used to copy content of the provided file into the current file.

Then, we have stdio.h. This is called header file. They are used to take take standard input output . The functions such as printf() and scanf() are included in this header file.

  • After that, we have main function.
int main() {
     . . .
}

This is the declaration of main function. The execution of C program starts with main() so, every C program must have a main().

Here, int specifies that the main function returns a integer value after the execution.

  • We use the function printf() to display text on the screen.
printf(“Hello World!”);

printf() function is used to  display the string passed in the screen. Here, we passed “Hello World!”. So, “Hello World!” will be displayed on the screen (without quotation marks).

  • Earlier we said that the main function returns an integer value; return is used to return a value.
return 0;

Here 0 is returned. General convention is that if the program executes successfully it returns 0 else 1.

  • Every statement in C program ends with a semicolon (“;”). We must end a statement with a semicolon in a C program.

Leave a Reply

Your email address will not be published. Required fields are marked *