In C++, every program has a basic structure that includes a main function called main()
. This function is the entry point for the execution of the program.
Let’s take a look at the basic structure of a program
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Here
#include <iostream>
allows the use ofstd::cout
to display text on the console.int main()
is the main function of the program.std::cout
prints text to the console, followed bystd::endl
for a newline.return 0;
indicates that the program finished successfully.
Semicolon to end statements
In C++, every statement must end with a semicolon (;
). This semicolon indicates the end of an instruction and helps the compiler interpret the code correctly.
int x = 10; // Statement ended correctly
std::cout << x << std::endl; // Statement ended correctly
If the semicolon is omitted, the compiler will generate an error.
Braces to delimit code blocks
In C++, braces {}
are used to group statements into code blocks (especially in control structures like functions, loops, or conditions).
All the code within the braces belongs to the same block and is executed together.
if (x > 0) {
std::cout << "x is positive" << std::endl;
} else {
std::cout << "x is not positive" << std::endl;
}
Here, the code inside the braces {}
of each block (if
and else
) will execute based on the established condition.
Case sensitivity
C++ is a case-sensitive language, which means it distinguishes between uppercase and lowercase. For example, variable
, Variable
, and VARIABLE
would be three distinct identifiers.
int age = 25;
int Age = 30;
std::cout << age << std::endl; // Prints 25
std::cout << Age << std::endl; // Prints 30
It is important to be consistent with the use of uppercase and lowercase to avoid errors.
Naming conventions
Like other languages, C++ has rules for naming variables and functions:
Cannot start with a number: variable names must start with a letter or an underscore (
_
), but never with a number.int 1age = 25; // Incorrect int age1 = 25; // Correct
Cannot contain spaces: names must be a single word without spaces.
int my age = 25; // Incorrect int myAge = 25; // Correct
Cannot include special symbols like
@
,#
,%
,!
, etc., but can use underscores (_
).int age$ = 25; // Incorrect int _age = 25; // Correct
Style conventions: although C++ does not require it, it is common to use camelCase convention (like
myVariable
) or snake_case (likemy_variable
) for variable names, depending on the team’s or project’s style.
Reserved words
C++ has a set of reserved words that cannot be used as variable names, functions, or identifiers, as they are predefined by the language to perform specific tasks.
Some of the reserved words in C++ include:
- Data types:
int
,float
,double
,char
,void
,bool
- Control flow:
if
,else
,switch
,case
,default
- Loops:
for
,while
,do
,break
,continue
- Variable and function declaration:
return
,const
,auto
,static
,extern
,register
- Class and object handling:
class
,struct
,public
,private
,protected
,virtual
,this
- Logical and assignment operators:
new
,delete
,sizeof
,typedef
,namespace