C++ Basic Syntax

The basic syntax in C++ encompasses the rules and conventions that govern how code is written and structured in the language. Here are some essential aspects of C++ syntax:

Statements and Semicolons

In C++, statements are terminated with semicolons.

int age = 30; // Declaration and assignment statement std::cout << "Hello, World!"; // Output statement

Curly Braces

Curly braces {} are used to define code blocks and indicate the beginning and end of functions, loops, and other program structures.

int main() { // Code block // ... }


Comments are used for explanatory notes in the code and are preceded by double slashes (//) for single-line comments or enclosed between /* and */ for multi-line comments.

// This is a single-line comment /* This is a multi-line comment spanning multiple lines. */


Identifiers are used to name variables, functions, classes, and other program elements. They must start with a letter or underscore and can contain letters, digits, and underscores. Case sensitivity matters in C++.

int myVariable; // Variable with an identifier void myFunction(); // Function with an identifier

Data types

C++ supports a variety of data types, including primitive types (such as int, float, and char) and composite types (such as struct and class). The data type of a variable determines what kind of data it can store.

int my_integer_variable; // This variable can store an integer value. float my_float_variable; // This variable can store a floating-point value. char my_character_variable; // This variable can store a single character.


Variables are used to store data. In C++, variables must be declared before they can be used. The type of the variable must also be specified.

int my_variable = 10; // This declares a variable of type `int` and assigns it the value 10. // You can also assign a value to a variable when you declare it. float my_other_variable = 5.5f; // This declares a variable of type `float` and assigns it the value 5.5.


Operators are used to perform operations on data. C++ supports a variety of operators, including arithmetic operators, relational operators, and logical operators.

// Arithmetic operators int sum = 10 + 5; // This expression evaluates to 15. int difference = 10 - 5; // This expression evaluates to 5. int product = 10 * 5; // This expression evaluates to 50. float quotient = 10.0f / 5.0f; // This expression evaluates to 2.0.
// Relational operators bool is_greater = 10 > 5; // This expression evaluates to true. bool is_equal = 10 == 5; // This expression evaluates to false.
// Logical operators bool is_and = true && true; // This expression evaluates to true. bool is_or = true false; // This expression evaluates to true.


Expressions are used to evaluate values. Expressions can be composed of variables, operators, and constants.

// This expression evaluates to the sum of two variables: int sum = my_variable1 + my_variable2; // This expression evaluates to the product of a variable and a constant: float product = my_float_variable * 3.14f;

Control Flow Statements

Statements are used to tell the computer what to do. C++ supports a variety of statements, including control flow statements (such as if, while, and for), function calls, and declarations.

// This statement prints the value of the variable `my_variable` to the console: std::cout << my_variable << std::endl; // This statement checks if a variable is greater than another variable and prints a message if it is: if (my_variable1 > my_variable2) { std::cout << "my_variable1 is greater than my_variable2." << std::endl; } // This statement loops until a condition is met: while (my_variable < 10) { std::cout << my_variable << std::endl; my_variable++; }


Functions are used to organize code into reusable blocks. Functions can take parameters and return values.

// This function calculates the factorial of a number: int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } // This statement calls the `factorial()` function and prints the result to the console


C++ has a set of reserved keywords that cannot be used as identifiers because they have predefined meanings in the language. Examples of keywords include int, for, if, and class.


Whitespace, including spaces, tabs, and line breaks, is used for formatting and is generally ignored by the compiler. However, it helps make code more readable.

Case Sensitivity

C++ is case-sensitive, meaning that identifiers like myVariable and MyVariable are considered distinct.

Preprocessor Directives

Preprocessor directives start with a '#' symbol and are used to include libraries, define constants, and perform other pre-compilation tasks.

#include <iostream> // Include the iostream library #define PI 3.14159 // Define a constant


Basic syntax in C++ encompasses fundamental rules for writing code, including the use of semicolons to terminate statements, curly braces for code blocks, comments for explanations, and conventions for naming identifiers. Understanding these elements is crucial for writing structured and functional C++ programs while following the language's case sensitivity and preprocessor directives to manage code dependencies and constants.