C++ Basic Syntax

C++ is a powerful, general-purpose programming language. Understanding its basic syntax is the first step to writing correct and efficient programs. This guide explains the core building blocks: program structure, comments, variables, data types, input/output, and simple operators — with clear examples.

1. Your First Program: Hello World

Every C++ program starts with including necessary headers and defining the main() function — the entry point of execution.

C++
Classic Hello World Program
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Key points: Every statement ends with ;, code blocks use { }, and main() returns int (usually 0 for success).

2. Comments in C++

Comments help explain code and are ignored by the compiler.

  • // Single-line comment
  • /* Multi-line comment */ — can span several lines.
C++
Example with comments
#include <iostream>
using namespace std;

int main() {
    // This is a single-line comment
    /* This is a
       multi-line comment */
    cout << "Commented code runs fine!";
    return 0;
}

3. Variables and Basic Data Types

Variables store data. You must declare a type before using them.

  • int → whole numbers (e.g., 42, -10)
  • float / double → decimal numbers (double is more precise)
  • char → single character (e.g., 'A')
  • bool → true or false
  • string → text (requires #include )
C++
Declaring and using variables
#include <iostream>
#include <string>
using namespace std;

int main() {
    int age = 25;
    double salary = 50000.75;
    char grade = 'A';
    bool isStudent = true;
    string name = "Alex";

    cout << "Name: " << name << ", Age: " << age << endl;
    return 0;
}

4. Input and Output (cin & cout)

cout prints to screen. cin reads from keyboard.

C++
Taking input from user
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Hello " << name << "! You are " << age << " years old." << endl;
    return 0;
}

5. Basic Operators

C++ supports arithmetic, relational, logical, and assignment operators.

  • Arithmetic: + - * / %
  • Assignment: = += -= *= /=
  • Comparison: == != > < >= <=
  • Logical: && || !
C++
Operators example
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;

    cout << "Sum: " << a + b << endl;
    cout << "Division: " << a / b << " (integer division)" << endl;
    cout << "Modulus: " << a % b << endl;

    bool isGreater = (a > b);
    cout << "Is a > b? " << isGreater << endl;

    return 0;
}

6. Writing Code Without 'using namespace std;'

It's better practice to use std:: prefix instead of importing the entire namespace.

C++
Hello World without namespace
#include <iostream>

int main() {
    std::cout << "Hello without namespace!" << std::endl;
    return 0;
}

7. Compiling and Running C++ Code

Use g++ (GNU C++ compiler) — widely available on Linux, macOS, and Windows (via MinGW or WSL).

BASH
Compile and execute
g++ myprogram.cpp -o myprogram
./myprogram          # on Linux/macOS
myprogram.exe       # on Windows

Conclusion & Next Steps

You've now learned the core syntax of C++ — program structure, comments, variables, basic I/O, and operators. Practice these concepts by writing small programs. Next, explore control structures (if-else, loops), functions, and arrays to build real applications.

Happy coding!