Language: EN

cpp-sintaxis-basica

Basic Syntax of C++

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 of std::cout to display text on the console.
  • int main() is the main function of the program.
  • std::cout prints text to the console, followed by std::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 correctly interpret the code.

int x = 10; // Statement properly ended
std::cout << x << std::endl; // Statement properly ended

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 such as 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 according to 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 Rules

Like other languages, C++ has rules for naming variables and functions:

  1. Cannot start with a number: variable names must begin with a letter or an underscore (_), but never with a number.

    int 1age = 25; // Incorrect
    int age1 = 25; // Correct
  2. Cannot contain spaces: names must be a single word without spaces.

    int my age = 25; // Incorrect
    int myAge = 25; // Correct
  3. Cannot include special symbols like @, #, %, !, etc., but they can use underscores (_).

    int age$ = 25; // Incorrect
    int _age = 25; // Correct
  4. Style conventions: although C++ does not require it, it is common to use camelCase convention (like myVariable) or snake_case (like my_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, function names, or identifiers, as they are predefined by the language for 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
  • Handling classes and objects: class, struct, public, private, protected, virtual, this
  • Logical and assignment operators: new, delete, sizeof, typedef, namespace