Skip to content

Basic Syntax of C++

In this section, we will cover the basic syntax and structure of a C++ program. Understanding the fundamental elements of C++ syntax is essential for writing and reading C++ code effectively.

Structure of a C++ Program

A basic C++ program consists of the following components:

  1. Preprocessor Directives: Instructions that are processed before the actual compilation begins.
  2. Main Function: The entry point of the program.
  3. Statements and Expressions: The code that performs the operations of the program.

Example of a Simple C++ Program

#include <iostream>  // Preprocessor directive

int main() {  // Main function
    std::cout << "Hello, World!" << std::endl;  // Statement
    return 0;  // Statement
}

Preprocessor Directives

Preprocessor directives begin with a # symbol and are processed before the compilation of the code. They are used to include files, define constants, and set conditions for compilation.

Common Preprocessor Directives

  • #include: Includes the contents of a file.
  • #define: Defines a constant or a macro.
  • #if, #else, #endif: Conditional compilation directives.

Example

#include <iostream>  // Includes the iostream library

#define PI 3.14159  // Defines a constant for PI

int main() {
    std::cout << "Value of PI: " << PI << std::endl;
    return 0;
}

The Main Function

The main function is the entry point of a C++ program. Every C++ program must have a main function. It has the following structure:

int main() {
    // Code to be executed
    return 0;
}

The main function returns an integer value. The return 0; statement indicates that the program executed successfully.


Statements and Expressions

Statements are the instructions that a C++ program executes. Each statement ends with a semicolon (;). Expressions are combinations of variables, constants, and operators that are evaluated to produce a value.

Example

int a = 5;  // Declaration and initialization of a variable
int b = 10;  // Declaration and initialization of another variable
int sum = a + b;  // Expression that adds two variables

std::cout << "Sum: " << sum << std::endl;  // Output the result

Comments

Comments are used to explain the code and are ignored by the compiler. C++ supports two types of comments:

  • Single-line comments: Start with // and extend to the end of the line.
  • Multi-line comments: Start with /* and end with */.

Example

// This is a single-line comment

/*
This is a
multi-line comment
*/

Variables and Data Types

Variables are used to store data that can be manipulated by the program. Each variable must have a data type, which defines the type of data it can hold.

Common Data Types

  • int: Integer
  • float: Floating-point number
  • double: Double-precision floating-point number
  • char: Character
  • bool: Boolean

Example

int age = 25;  // Integer variable
float height = 5.9;  // Floating-point variable
double weight = 70.5;  // Double-precision variable
char grade = 'A';  // Character variable
bool isStudent = true;  // Boolean variable

Input and Output

C++ provides the iostream library for input and output operations. The two main objects used for this purpose are:

  • std::cout: Standard output stream (usually the console)
  • std::cin: Standard input stream (usually the keyboard)

Example

#include <iostream>

int main() {
    int age;
    std::cout << "Enter your age: ";  // Output to the console
    std::cin >> age;  // Input from the keyboard
    std::cout << "You are " << age << " years old." << std::endl;
    return 0;
}

Next Steps

To continue learning about C++ programming, proceed with the following topics:

  1. Data Types and Variables: Understand the different data types and how to declare variables.
  2. Control Structures: Explore the various control structures like loops and conditional statements.
  3. Functions: Learn how to define and use functions in C++.

By mastering these basic syntax elements, you will be well-prepared to write and understand more complex C++ programs.

Next