C program to print Hello World

In this example, we will learn step by step how to print “Hello World” in C. It has always been like a tradition, that when you start learning a new programming language, you start with the program that prints the message “Hello World”.

#include <stdio.h>
int main()
{
    //The printf() function will display the string inside quotation
    printf("Hello World");
    return 0;
}

Output

Hello World

How this Program works –

  • The #include is a preprocessor directive. Preprocessor directives are not program statements but directives for the preprocessor. A preprocessor directive is processed before the compilation of program. In this program the #include is used to include the header file stdio. This file contains the functions printf  which is used to print output on the screen. If you use this function without including the stdio header file, the compiler will generate an error.
  • Every C program starts with the main function. The curly braces { and } represents the starting and ending of the function. the main function is declared as ‘int main’, where int is the return type of the main function. This means this function will return an integer value.
  • The line written after // is a comment. A comment is not executed by the compiler. Comments are used to make the program understandable.
  • printf() is an inbuilt function of C language that prints the argument passed inside it, as output on the screen. The printf() function resides within the stdio header file.
  • A string “Hello World” has been passed as an argument to the printf function. Therefore it would print the message “Hello World” when the program is executed. A string value needs to be placed inside quotations as in our program.
  • return 0 is the ending point of our program. As the main function has declared of return type int, it has to return an integer value. But our program is not returning anything. Therefore a statement return 0 is needed.

Note: If you don’t want to return 0, you can declare your main function like this:

#include <stdio.h>
void main()
{
    //The printf() function will display the string inside quotation
    printf("Hello World");
}

void means NULL, that means this program will return nothing.