This section is important, before diving into the main content of the C++ program, you need to learn the basic syntax of C++. You need to know in which manner C++ code is written. C++ is the collection of classes and objects that communicate with each other via. method.
C++ Program Structure
Let us start by looking at the simple hello world program in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Header file for input/output #include<iostream> using namespace std; // main function from where the execution of program starts int main() { //prints hello world cout<<"Hello World"; return 0; } |
// Header file for input/output: The line starting with // are the comment lines that are not included during execution but these are for the programmer to comment on the necessary section as required. It is considered a good practice to use comments. For multiple comment lines, you start with /* and end with */ and write in between.
#include<iostream>
: This is the header files used for executing the standard input and output function in C++. There are several header files that are very useful for the program. The line starting with # are directives that are processed by the preprocessor.
Some of the header files used in C++:
<iostream> | Contains C++ standard input and output functions |
<cmath> | Contains math library functions |
<ctime> | Contains function for manipulating the time and date |
<fstream> | Contains function for functions that perform input from files on disk and output to files on disk |
using namespace std;: This line tells the compiler to use a group of functions that are part of the standard library (std). A semi-colon ‘;’ is used to end a statement.
int main():
It is the main function of the program from where the execution of the program begins. int
is a type that shows that the function returns integer type. If you don’t want to return anything then simply use the void
instead of int.
{ ..... }
: Opening and Closing braces shows the beginning and the ending of the main function in a program.
cout<<"Hello World";
: This line is responsible for displaying the Hello World on the screen. cout
is the standard out function present in <iostream>
header file.
return 0;
: This statement in a program is used to return a value from the function and also indicating the end of the function. You can usually use it to return some result of the calculation and such.
Compilation and Execution of C++ Program
There are steps to compile and run the C++ program n your computer. You need to save, compile and run, follow the below steps.
- First, open a text editor(eg notepad) on your computer and write your C++ code.
- Now save the file as
file_name.cpp
. Example: hello.cpp. - Then open a command prompt and go to the directory where you have saved the file.
- And on the command prompt type
'g++ hello.cpp
‘ and press enter to compile your code. And if no error is found then the command prompt will take you to the next line. - Now, type ‘
a.out
‘ to run your program. - Then Hello world will be printed on the screen (considering the above code).
It will look something like this:
1 2 3 | $ g++ hello.cpp $ ./a.out Hello World |
Always make sure that you are in the same directory where you have saved the file.