⬅ Previous Next ➡

Basic Concepts & Syntax

Basic Concepts & Syntax in C++
  • Core C++ Syntax includes tokens, keywords, identifiers, variables, constants, data types, type casting, operators, and operator precedence.
  • C++ is case-sensitive and uses ; to end most statements.

1) Tokens in C++

  • Tokens are the smallest building blocks of a program.
  • Main token types:
    • Keywords (reserved words)
    • Identifiers (names)
    • Literals/Constants (fixed values)
    • Operators (+, -, *, >>, <<, etc.)
    • Punctuators (; , { } ( ) [ ])

2) Keywords

  • Keywords are reserved and cannot be used as variable names.
  • Examples: int, float, double, char, bool, if, else, for, while, class, public, private, return, new, delete.

3) Identifiers

  • Identifiers are user-defined names for variables, functions, classes, etc.
  • Rules:
    • Start with letter or underscore
    • Can contain letters, digits, underscore
    • No spaces or special symbols
    • Cannot be a keyword

4) Variables & Constants

  • Variable: stores a value that can change.
  • Constant: fixed value (cannot change).
  • Use const keyword for constants.
#include <iostream>
using namespace std;

int main() {
    int age = 20;           // variable
    const float PI = 3.14f; // constant

    age = 21; // allowed
    // PI = 3.1415f; // not allowed

    cout << "Age: " << age << endl;
    cout << "PI: " << PI << endl;
    return 0;
}

5) Data Types

  • int (integer), float (decimal), double (more precision), char (character), bool (true/false).
  • string for text (from <string> or included via iostream in many compilers).
  • Modifiers: short, long, signed, unsigned.
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    float b = 2.5f;
    double c = 99.99;
    char ch = 'A';
    bool ok = true;

    cout << a << " " << b << " " << c << " " << ch << " " << ok << endl;
    return 0;
}

6) Type Casting (Implicit & Explicit)

  • Implicit: automatic conversion (int → double).
  • Explicit: manual conversion using (type) or static_cast<type>.
#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 2;

    double div1 = a / b; // integer division first -> 2
    double div2 = (double)a / b; // explicit cast -> 2.5
    double div3 = static_cast<double>(a) / b; // best C++ style

    cout << "div1: " << div1 << endl;
    cout << "div2: " << div2 << endl;
    cout << "div3: " << div3 << endl;

    return 0;
}

7) Operators in C++

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, *=, /=, %=
  • Unary: ++, --, +, -
  • Bitwise: &, |, ^, ~, <<, >>
  • Ternary: condition ? x : y
#include <iostream>
using namespace std;

int main() {
    int x = 10, y = 3;

    cout << "x+y = " << (x + y) << endl;
    cout << "x%y = " << (x % y) << endl;
    cout << "x>y = " << (x > y) << endl;
    cout << "(x>5 && y<5) = " << ((x > 5) && (y < 5)) << endl;

    int maxVal = (x > y) ? x : y;
    cout << "max = " << maxVal << endl;

    return 0;
}

8) Operator Precedence & Associativity

  • Precedence decides which operator runs first.
  • Associativity decides direction when same precedence exists.
  • Common order (high → low):
    • ()
    • Unary (++ -- ! ~)
    • * / %
    • + -
    • << >>
    • < <= > >=
    • == !=
    • & then ^ then |
    • && then ||
    • ?:
    • =, +=, -= ... (right-to-left)
  • Use parentheses () to avoid confusion.
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5, c = 2;

    int ans1 = a + b * c;     // * first -> 10 + 10 = 20
    int ans2 = (a + b) * c;   // () first -> 15 * 2 = 30

    cout << "ans1 = " << ans1 << endl;
    cout << "ans2 = " << ans2 << endl;

    return 0;
}
⬅ Previous Next ➡