C++ First Program

Let’s write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler.

Open any editor to write C++ program.

Output of above program

Compile and Run

After writing C++ program, save the program with any name (first) with .cpp extension (first.cpp).

Open any command prompt (MinGW Command Prompt) window and go to directory structure where C++ program file is saved.

Write a command to compile C++ program using command g++ first.cpp (g++ file_name.cpp). This command generates an executable file with default name a at same directory.

After that write a command to run the program with command a (generated .exe file name).

If we want to generate executable (.exe) file with different name, write a command g++ first.cpp –o first. This generates an executable file of name first.

To run this generated .exe file simply type command first or first.exe.

 

Let’s understand the above code structure.

#include refers a preprocessor directive used to include header file, other files etc. before compiling the program.

<iostream> is header file contains the input stream, output stream functions.

using namespace std is used for using namespace std, where features of string or vector, are declared.

main() is the function from where the program main execution starts and int is the return type of function main(), int returns integer value.

cout<< is output stream use to display data on the screen. Its working functionality is provided in <iostream> header file.