C++ programs have the extension .cpp
. The names of the files are system dependent.
In order for a computer to execute a program, it requires a well-defined entry point. In C++, this entry point is the main()
function, which serves as the starting point of program execution. A basic C++ program begins with the following structure:
int main() { // Your code here }
The execution of the program starts at the first statement inside the curly braces {}
, and the program reads and executes each line sequentially, from top to bottom/left to right, within this block of code. A block of code refers to a structured grouping of multiple statements or instructions that execute as a single unit.
If the main()
function is forgotten in a .cpp
file when compiling an error similar to the following will occur:
ld: Undefined symbols: _main, referenced from: <initial-undefines> clang: error: linker command failed with exit code 1 (use -v to see invocation)
If the main function is included in the file and this error occurs ensure the file is saved prior to compiling.
At this stage, the main()
function does not perform any operation. The simplest addition to a C++ program is a return statement, which indicates the program has completed execution:
int main() { return 0; }
The statement return 0;
tells the computer to terminate the program successfully. Any code written after this statement will not be executed. It is important to note that each statement in C++ must end with a semicolon (;
), which signifies the conclusion of an instruction.
To make the program produce an output, we can use std::cout
from the iostream library to display a message on the screen:
#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
Here, #include <iostream>
is a preprocessor directive – special instructions in a programming language, primarily used in C and C++, that are executed by the preprocessor before the actual compilation of the program begins. The preprocessor in C++ is a tool that processes source code before compilation by handling directives (e.g., #include, #define) for file inclusion, macro substitution, and conditional compilation.
In this case the preprocessor directive instructs the compiler to import precompiled code from the iostream
library that enables input and output operations. The statement std::cout << "Hello, World!" << std::endl;
is allows for the std::cout
, <<
, and "std::endl" responsible for displaying string "Hello, World!"
on the screen.
You may notice the usage of std::
before cout
. This is known as a namespace, which helps organize names of different functions and objects in C++. To simplify the syntax and avoid writing std::
repeatedly, the using directive can be included:
#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
With using namespace std;
, the compiler automatically assumes that functions such as cout
belong to the standard library (std), eliminating the need to prefix them with std::
.
Once the source code is written, it must be compiled before execution. Compilation is the process of converting human-readable source code into machine-readable code that the computer can understand. This is accomplished using a compiler, such as the GNU Compiler Collection (GCC).
To compile a C++ program using GCC, execute the following command in the terminal:
g++ filename.cpp
(Replace filename.cpp
with the actual name of your source file.)
When the compiler processes the source code, several key steps occur:
#include <iostream>
) and expands macros.iostream
).If there is a syntax error in the program the compiler will halt compilation and output a relevant error message.
After successful compilation, the compiler generates an executable object file, typically named a.out
by default. This file contains the machine-level instructions required to run the program. To change the name of the object file add to the compile command -o name
:
g++ filename.cpp -o name
(Replace name
with the object file name wanted.)
This will cause a.out
to be renamed to name
.
Once the compilation is complete, the program must be loaded into memory before execution. This is done by the loader, a system component responsible for loading executable code into the computer’s main memory (RAM).
To run the compiled program, execute the following command in the terminal:
./a.out
(If you specified a different output filename during compilation, replace a.out
with that filename.)
Write the C++ source code using a text editor or an Integrated Development Environment (IDE).
Compile the source code into machine code using a compiler:
Execute the compiled program by running the generated executable file.
Program Entry Point - The starting point of program execution, defined as main().
Sequential Execution - The order in which statements are executed, from top to bottom/left to right.
Block of Code - A structured group of statements that execute as a unit.
Semicolon (;) - Marks the end of a statement in C++.
Compiler - A tool that translates source code into machine code.
Preprocessor Directive - Special instructions in a programming language, primarily used in C and C++, that are executed by the preprocessor before the actual compilation of the program begins.
Preprocessor - A tool that processes source code before compilation by handling directives (e.g., #include, #define) for file inclusion, macro substitution, and conditional compilation.
Linker - Combines object files with necessary libraries.
Loader - Loads the compiled program into memory for execution.
What is the entry point of a C++ program, and why is it necessary?
What would happen if a C++ program did not include a main() function?
How does the sequential execution of statements within main()
affect program behavior?
Why must each statement in C++ end with a semicolon (;)?
What is the purpose of the return statement in the main()
function?
What happens to any code written after the return 0;
statement inside main()
?
What is a block of code, and how is it represented in C++?
What is a namespace, and why is it used in C++?
How does using using namespace std;
simplify code?
Why must a C++ program be compiled before execution?
What command is used to compile a C++ program using GCC?
If a C++ source file is named program.cpp
, what exact command would you use to compile it?
What happens if there is a syntax error in a C++ program during compilation?
What are the three main stages of the compilation process?
What occurs during the preprocessing stage of compilation?
What is the role of the linker in the compilation process?
Why is linking necessary when compiling a C++ program?
What is an executable file, and what is its default name when compiled using GCC?
What does the compiler generate after a successful compilation of a C++ program?
What is the role of the loader in executing a compiled program?
What command is used to execute a compiled C++ program on a Linux terminal?
If a program was compiled using the command g++ myfile.cpp -o myprogram
, how would you execute it?