In C++, every program has a basic structure that includes a main function called main(). This function is the starting point of program execution.
Let’s 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::coutto display text on the console.int main()is the main function of the program.std::coutprints text to the console, followed bystd::endlfor a line break.return 0;indicates that the program terminated successfully.
Semicolon to End Statements
In C++, each 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 instructions into code blocks (especially in control structures like functions, loops, or conditions).
All code inside 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 according to the established condition.
Case Sensitivity
C++ is a case-sensitive language, meaning 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 Rules
As in 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 they can use underscores (_).
int age$ = 25; // Incorrect
int _age = 25; // Correct
- Style conventions: although C++ does not require it, it is common to use the camelCase convention (as in
myVariable) or snake_case (as inmy_variable) for variable names, depending on the team or project style.
Reserved Words
C++ has a set of reserved words that cannot be used as names for variables, functions, or identifiers, as they are predefined by the language for specific tasks.
Some of C++‘s reserved words include:
- Data types:
int,float,double,char,void,bool - Flow control:
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
