Notes / C++ / Variables

Variables in C++ — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Variables Just like C, C++ variables must have a declared type.

Syntax

syntax
type variableName = value;
auto variableName = value;  // type inferred

Example

cpp
#include <iostream>
using namespace std;

int main() {
    int age = 20;
    auto price = 49.99;   // inferred as double
    cout << "Age: " << age << endl;
    cout << "Price: " << price << endl;
    return 0;
}

Output

outputAge: 20 Price: 49.99

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview Know when NOT to use auto — for readability, explicit types are often clearer for public function signatures.
Back to notes Read full lesson →