Check The Programming Section
This program outputs the message, "Hello, World!" into the computer screen.
#include<stdio.h>
int main(){
printf("Hello, World!\n");
return 0;
}
Consider the code, as a set of instruction telling the computer to perform a specific task and produce the outcome of these instructions as "Hello, World!" to the computer screen. These set of instructions are like a recipe to a cook to follow. Let's discuss each line of this program.
Computer will look for a function called main and starts executing an instruction it finds there. Here is the function main of our Hello World! pogram
int main(){
printf("Hello, World!\n"); \\line 1
return 0; \\line 2
}
Every C++ program must have a main function, which tells the computer, from where to start the execution of the program. A function is basically a named sequence of instructions for the computer to execute in the same order in which it is written.
A function has a name, in this case main
Function has a return type, here is int (means integer). A return type specifies, which type of result (if have) a function may return to whoever called the function for execution. In this case, main function gets called by operating system of the computer and a value zero (0) is returned by the statement return 0;.
A list of parameter enclosed in parentheses, here (). In this program, list of parameter is empty.
A function body, enclosed in a set of "curly braces", { }, contains the set of instruction to be executed.
int main(){ }
The body of the main function in our program have two statements:
printf("Hello, World!\n");
return 0;
The first line prints the "Hello, World!" to the computer screen and placed the cursor at the start of the next line. The escape sequence '\n' is responsible to move the cursor into the next line. To print "Hello, World!", the printf function is used. And the second line returns a value zero (0) to whoever called it.
Since main() is called by the operating system we won't use that return value zero (0). However, on some systems (notably Unix/Linux) it can be used to check whether the program execution is succeeded or not. A zero (0) returned by main() indicates the program execution is terminated successfully.
To use any library function, we must use #include pre-processor directives to add a header or a header file (in some language it is represent as library file) with extension .h in C/C++ program. In this program, the first line #include<stdio.h> tells the compiler, any part of the program might use a library function, which is defined insdie this header file.
The printf() inside the main function requires stdio.h header file, which have the required pre-compiled source code of printf function. Inside the printf function, the double quotes " " is used, to pass a string literal as argument to the printf function.
printf is a standard library function, used to sends formatted string to the standard output device a computer screen. We will learn more about printf() in input and output section.