structure of a program in C++

In this page, we will discuss following topics,

 

header file

In C++, before making a program we include some system library file in the header of the program. these file are called header file. These files contain a lot of functions that have a code to perform a task. To use these functions in a program, we have to include their file (within which it define) in the program header.

Here is syntex to include a header file in the program.

SYNTEX:-

#include<filename.h>

here .h is a file extension.
Here are the names of some header files and the operations or tasks they perform.

Yes, it is possible that a function can be defined in more than one header file like exit () functions are defined in both header file stlib.h and process.h.

All these functions are used further in this tutorial.

cout statement / output statement

cout is an output statement that is used to print a message or result on an output device (like monitor screen).
Example–

cout<<“message”;

<< called output operator or insertion operator.

cin statement / input statement

cin is an input statement that reads the value of a variable at run-time, when the program is executed, values are given by an Output device (such as keyboard).
Example–

cin>>variable1;
cin>>variable2;

or

cin>>variable1>>variable2;

In the variable, the store value is printed by the output statement (cout statement).
Example

cout<<variable; 

>operator called input operator or extraction operator

Simple structure of a c++ program

In C++, a program is divided into two part first called header- file and second void main(), within the main() function, the code is written. such as

program

Here is the program,

#include<iostream.h>
#include<conio.h>

void main()
{
  cout<<"Hello world";
  getch();
}
OUTPUT:-
hello world

Explanation:

iostream.h and conio.h, both are header file.
void is a return type, in which main() is a function that is the starting point of a program. Here the void type data type indicates that main() fuction is not returning any value.
getch() is a fuction that holds the output screen.

thing to know

  • In C ++, both statements (cout, cin) are terminated by semicolon (;).
  • Both cout and cin are define under in iostream class so we have to declare their header file (iostream.h) before using them.
  • In cout statement, the message is written within double quotes, whereas in the cin statement double quotes are not used to read the value of any variable.

previous-a brief introduction about C++

next-Token and keyword