C hello world program – Every line of code explained

Hello world program in C with example. Every line of code is explained and have code comments. This is a simple program that display Hello World on console screen using printf function. this function is a standard library function.

C hello world program example:


/*----------------------------------------
* C Hello World program example
*/

//include stdio.h header file as we want to use
// printf() function to print hellow world on console.
#include<stdio.h>
/*
* compiler automatically look for main()function as
* a starting point. Compiler always start from main in 
* the program
*/

// in int main(), it is the return type as program will
//Return int value 0 to tell OS that program is successful.
int main () {

	/*
	* Print hello world on console window
	* using printf function by passing
	* Hellow World !!! string.
	*/ 

	printf("Hello World !!!");

	//By returning 0, tell operating system or shell
	//that program is successfully completed.
	return 0;
}

Output:

Hello World !!!

NOTES:

Every program should contain main function as this is the entry point of the C program. When we execute the program, compiler find the main() function and start executing from here.

Doubt: What is  int main() and void main()?

int and void both are return type. it shows that main function is returning some int value. in case of void main(), function is not returning anything.

Using int main() returns an exit value to the user shell or operating system whereas void main() technically doesn’t.

In other words, main() should return 0 or 1. If it returns 0 to the operation system, the operating system will understand the program is successfully executed. If it returns 1, the operating system will understand that the program is terminated with error. So main() should not be declared as void.

Related Posts