Basic Structure of C++ Program - The Coding Shala

Home >> Learn C++ >> Basic Structure of C++ Program

Basic Structure of the C++ Program

The basic structure of the C++ program is as follows - 
Basic Structure of C++ Program - The Coding Shala
C++ program can be simplified into two parts - first, writing a program in a text editor and save it with the correct extension. c++ program can be save with .cpp, .c, .cp extension.
second, compiling your c++ program using a compiler or online IDE to check the output of your program.

Let's look the below c++ code that will print 'Hello World' as output -  

1:  //This is comment inline comment  
2:  /* This is also comment mulitine comment  
3:   C++ program to display "Hello World"  
4:   end of multiline comment */  
5:  #include<iostream> // this include header file for input output functions  
6:  using namespace std;  
7:  // main function starts where the execution of program begins  
8:  int main()  
9:  {  
10:    cout<<"Hello World";  // prints heelo world  
11:     //semicolon is used as statement terminater.  
12:    return 0;  
13:  }  

Output : 
1:  Hello World  



Now, let's understand every line of the c++ program - 

In C++ there are two types of comment, single-line comment, and multiline comments. The compiler always skips comments. Comments are used for additional information about the program. single-line comments start with '//' without quotes and multiline comments come between '/* .. */'. In our program line no. is contain single line comment and from line no. 2 to line no. 4 is a multiline comment.

line 5, the #include directive is used to include a file. Here #include<iostream>, tells the compiler to include the standard iostream file which contains a declaration of all the standard input/output library functions. 

line no. 6 - using namespace std, here we are going to use std namespace where features of the c++ standard library, such as string, vectors, etc. are declared means we can write just cout instead of std:: cout when calling the operator cout defined in the namespace std. we will learn more about namespace in the following chapters. 

line no. 8 - int main(), execution of every c++ program begins with the main() function so every c++ program must have a main() function. this line is used to declare the main function which returns data of integer type. It is never a good idea to use "void main()" or "main()" instead of "int main()" as it does not confirm standards. it may or may not be allowed by some compilers.

line no. 9 to 13 - between { .. }, '{' indicates the beginning of the main function and '}' the closing braces indicates the ending of the main function. this is the body of the main function. 

cout<<"Hello World";, this is a statement, every statement is meant to perform some task here, this display output 'Hello World' without quotes. 

return 0; this is also a statement. this statement is used to return a value from a function and indicates the finishing of a function. 

Every program is typically composed of 3 basic elements: expressions, statements, and functions. The most common type of instruction in a program is the statement. this is the smallest independent unit in the language for eg. int x; is a statement. An Expression specifies a computation to be performed and statements are typically grouped into units called functions. A statement is a complete sentence that tells the compiler to perform a particular task and expression is a mathematical entity that evaluates to a value.

Compile and Execute C++ Program 

In IDE we can directly compile and run the c++ program by simply run it. To compile the C++ program in command prompt follow the below steps - 
  • Open a text editor and write code inside it 
  • Save the file with a correct extension for eg. Hello.cpp
  • open the command prompt and go to the directory where the file is saved.
  • To compile code type 'g++ Hello.cpp' without quotes and press enter. here you will get to know errors if any errors are there otherwise it would generate a.out executable file.
  • To run the program type 'a.out' without quotes. it will display the outputs.


Other Posts You May Like
Prev<< Introduction to C++                                    NEXT >>Data Types and Modifiers in C++
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

Maximum Population Year LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala