A simple C++ program basically consists of the following parts −
- Preprocessor Commands
- Functions
- Variables
- Statements & Expressions
- Comments
Basic structure of C++ program
1 2 3 4 5 6 | #include <iostream.h> int main() { return 0; } |
Above shown the basic structure of C++ program.
For every program we have to writeabove mention basic structure.
How to write or print
We can write or print in C++ language by using “cout” object.
It is used to display the output to the standard output device i.e. monitor.
In C++, cout is an object of class ostream. for example: code to write “hello” is as follows:
cout << “hello”;
In “cout” , “c” refers to “character” and ‘out’ means “output”, means cout refers “character output”.
cout
object is used along with the insertion operator (<<) for display a stream of characters.
A simple C program to print “Hello”
1 2 3 4 5 6 7 8 9 10 | #include <iostream> int main() { /* first C++ program */ cout << "Hello World!"; return 0; } |
OUTPUT
1 | Hello World! |
Description
- First line #include <iostream.h> is a preprocessor, which instruct to C++ compiler to include iostream.h file. iostream ( input output stream ) header file contains definitions of objects like cin, cout etc.
- int main() is the main function from where the program execution begins.
- /*………..*/ is a “comments” in the program. This comments will be ignored by the compiler during execution of program
- cout is an object of ostream in C++ which is used to print message.
- line return 0; terminates the main() function and returns the value 0.return 0 tells the compiler that everything is ok and the execution of main() function ends. Because 0 is the code for successful execution, while values greater than 0 (integers) is an indication that something went wrong.