Hello World Program

Example

#include<stdio.h>       //header section

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




Output

Hello World




What is <stdio.h> ?

stdio.h is a header file which has the necessary information to include the input/output related functions in our program. Example printf, scanf etc.

If we want to use printf or scanf function in our program, we should include the stdio.h header file in our source code.


Otherwise, our program doesn’t know what is the definition of printf or scanf and it will throw error/warning saying that implicit declaration of built-in function ‘printf’




Why #include ?

#include is a preprocessor directory.

It will include the file which is given within the angle brackets "<>" into the current source file.

Example

If we use #include<stdio.h> in your c program, it will include stdio.h file into our source program which has the information for all input, output related functions.




Why int main()?

As we discussed earlier, the main function is the starting point of program execution.

Operating system (OS) initiates the program execution by invoking the main function.

And it will expect an integer value from the main function. That integer value represents the status of the program. That's why we declared main function return type as int.

If any error in the program while execution, the main function will return some non-zero value to the operating system.

If the program executed successfully without any error, It will return zero.






Why '{' ?

C is the block structured programming language where statements are grouped together to achieve specific task using curly braces.

Open curly brace { represents the starting point of the block.




printf(“Hello World”);

printf is a function which is defined in stdio.h header file. It will print everything to the screen which is given inside the double quotes.

In this case, Hello World will be printed as an output.

semicolon ";"

Every statement in C should end with semicolon ;.




Why return 0;?

As we declared main function return type as an integer, the calling function (operating system) will expect an integer value from the main function. In main function, we always explicitly return 0.

Program execution will reach return 0 statement if and only if all the above statements are executed without any error. If program execution reaches return 0 statement, We can ensure that there is no error in our program.

If some error occurred while program execution, the main function will return some non-zero value to the operating system to indicate the error.


Semicolon; indicates the end of the statement.




Why '}' ?

Close curly brace indicates the end of the block.


In other words, it indicates the end of main function.


Topics You Might Like