Program Structure
Understanding the basic structure of a program
C++ Program Structure
In this section, we will study the basic structure of a C++ program and become familiar with the main components that make up any program.
Basic Program Structure
Every C++ program consists of several key components:
Preprocessor Directives
Preprocessor directives begin with the # symbol and are processed before program compilation begins:
These directives tell the compiler which libraries need to be included in the program.
Namespace
This line allows us to use standard C++ objects (such as cout, cin, endl) without the std:: prefix. Without this line, we would have to write std::cout instead of cout.
The main() Function
The main() function is the entry point of the program:
int- indicates that the function returns an integermain- function name (required for any program)()- parameter list (empty in this case){}- function body where executable code is placedreturn 0;- returns 0, meaning successful program execution
Statements and Expressions
Inside the main() function, we write statements and expressions:
Comments
Comments help explain code and do not affect program execution:
Code Blocks
Curly braces {} group statements into blocks:
Code Writing Rules
- Every statement ends with a semicolon
; - C++ is case-sensitive -
Mainandmainare different names - Spaces and line breaks are ignored (but affect readability)
- Variable and function names can contain letters, digits, and underscores, but cannot start with a digit
This basic structure forms the foundation for all C++ programs that we will study going forward.