C++ Programming: From Basics to STL
Chapter 2 / 8· 20 min read· 0 cards

Variables, Data Types and Operators

How C++ stores numbers, characters, and text — and computes with them.

Declaring variables

Like C, C++ is statically typed — you declare what kind of data a variable holds before using it, and the compiler enforces it. This catches mistakes early and keeps programs fast:

int age = 19;          // whole number
double price = 49.99;  // decimal number (prefer double over float)
char grade = 'A';      // a single character (single quotes)
bool passed = true;    // true or false
string name = "Mehul"; // text (needs #include <string>)

C++ gives you a proper bool type (true/false) and a real string type — two big quality-of-life improvements over classic C, where you faked booleans with integers and strings with character arrays. The string type makes working with text far more pleasant, as you'll see.


The core types and their sizes

C++ exposes the size of each type, just like C — useful awareness for performance and for understanding overflow:

  • int — whole numbers, usually 4 bytes (about ±2 billion)
  • double — decimals with ~15 digits of precision, 8 bytes (the everyday choice for decimals)
  • char — a single character, 1 byte
  • bool — true or false, 1 byte
  • string — text of any length (from the standard library)
#include <iostream>
using namespace std;
int main() {
    cout << "int:    " << sizeof(int) << " bytes" << endl;
    cout << "double: " << sizeof(double) << " bytes" << endl;
    return 0;
}

A practical tip: prefer double over float for decimals unless you have a specific reason — the extra precision avoids subtle rounding bugs and the speed difference is negligible on modern hardware.


Arithmetic and the integer-division trap

The arithmetic operators are what you'd expect, but C++ inherits C's famous integer-division behaviour, which trips up every beginner once:

int a = 17, b = 5;
cout << a + b << endl;   // 22
cout << a * b << endl;   // 85
cout << a / b << endl;   // 3   -- integer division! decimal dropped
cout << a % b << endl;   // 2   -- remainder (modulo)

cout << (double)a / b << endl;  // 3.4 -- cast to double for real division

When both operands are integers, C++ throws away the decimal part: 17 / 5 is 3, not 3.4. To get a real answer, at least one side must be a floating-point number — the cast (double)a does exactly that. Remembering this single rule saves hours of debugging mysterious wrong answers.


Comparison, logical, and shortcut operators

Comparisons give a bool, and you combine conditions with && (and), || (or), ! (not):

int age = 25;
bool hasLicense = true;

cout << (age >= 18) << endl;          // 1 (true)
cout << (age == 25) << endl;          // 1  -- double equals compares!

if (age >= 18 && hasLicense) {
    cout << "Can drive" << endl;
}

Watch the classic = versus == trap — a single = assigns, a double == compares. C++ also has the handy shortcuts +=, -=, ++ (increment by one), and -- (decrement), which you'll use constantly in loops:

int score = 100;
score += 50;   // 150
score++;       // 151

The string type in action

One of C++'s biggest joys over C is the string type, which behaves like a first-class value — you can join strings with +, compare them with ==, and ask their length:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string first = "Mehul";
    string last = "Koshti";
    string full = first + " " + last;   // join with +

    cout << full << endl;                 // Mehul Koshti
    cout << "Length: " << full.length() << endl;  // 12
    cout << full[0] << endl;              // M (index like an array)

    if (first == "Mehul") {              // compare with ==
        cout << "Match!" << endl;
    }
    return 0;
}

Compare this to C, where joining strings needed strcat and comparing needed strcmp. The C++ string just works the way you'd hope. This pattern — the standard library giving you convenient, safe building blocks — is the heart of what makes C++ productive, and it reaches its peak in the STL we'll meet later. Next, control flow.

Reading mode · scroll to read at your own pace

Finished "Variables, Data Types and Operators"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.