Aral Code

Basics Lesson 4 of 17

C++ Variables, Data Types, auto and const

Declare C++ variables with int, double, char, bool and string, use auto for type deduction, const and constexpr for constants, brace initialization and casting with static_cast.

Variables store the data your program works with. C++ is statically typed: every variable has a type that's fixed at compile time. The compiler uses the type to reserve memory and to catch mistakes.

Basic types

TypeStoresExample
intWhole numbersint students = 42;
long longVery large whole numberslong long population = 115000000;
doubleDecimalsdouble average = 88.75;
charOne characterchar section = 'A';
booltrue or falsebool passed = true;
stringText (needs <string>)string name = "Ana";

Unlike C, C++ has a built-in bool type and a real string class.

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

int main() {
    string name = "Jose Rizal";
    int gradeLevel = 12;
    double average = 96.4;
    char section = 'A';
    bool isHonor = average >= 90;

    cout << name << ", " << gradeLevel << "-" << section << endl;
    cout << "Average: " << average << endl;
    cout << "Honor student? " << isHonor << endl;
    cout << boolalpha << "Honor student? " << isHonor << endl;
    return 0;
}

Output

Jose Rizal, 12-A
Average: 96.4
Honor student? 1
Honor student? true

By default, cout prints booleans as 1 and 0. boolalpha makes it print true and false.

Initialization styles

int a = 5;      // classic
int b(5);       // constructor style
int c{5};       // brace (uniform) initialization, C++11
int d{};        // zero: d is 0

Brace initialization is safer: int x{3.7}; is a compile error, because it would lose the decimal part. With int x = 3.7; the decimal is silently dropped.

auto

auto lets the compiler figure out the type from the value:

auto count = 10;          // int
auto price = 12.5;        // double
auto letter = 'Z';        // char
auto name = string("Ana");  // string

auto is handy for long type names (you'll appreciate it with the STL), but for simple variables, writing the type makes your code clearer while learning.

Constants

#include <iostream>
using namespace std;

constexpr double VAT = 0.12;       // known at compile time
const int PASSING = 75;

int main() {
    double price = 250;
    cout << "With VAT: " << price * (1 + VAT) << endl;
    cout << "Passing grade: " << PASSING << endl;
    // PASSING = 60;   // error: assignment of read-only variable
    return 0;
}

Output

With VAT: 280
Passing grade: 75

Prefer const and constexpr over C's #define: they have types and follow normal scope rules.

Integer division and casting

#include <iostream>
using namespace std;

int main() {
    int total = 265, subjects = 3;
    cout << total / subjects << endl;
    cout << static_cast<double>(total) / subjects << endl;
    cout << static_cast<int>(9.99) << endl;
    cout << static_cast<char>(66) << endl;
    return 0;
}

Output

88
88.3333
9
B

static_cast<type>(value) is the modern C++ cast. It's easier to search for and safer than C's (double) total, though both work.

Size and limits

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

int main() {
    cout << "int: " << sizeof(int) << " bytes, max " << INT_MAX << endl;
    cout << "long long max: " << LLONG_MAX << endl;
    return 0;
}

Output

int: 4 bytes, max 2147483647
long long max: 9223372036854775807

In programming contests, using int where long long is needed is a classic reason for "Wrong Answer". If values can exceed about 2 billion, use long long.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does cout << true; print by default?
2. What type does auto x = 4.5; give x?
3. Which is the modern C++ way to convert an int to double?
4. What happens with int x{3.7};?

By Sir Reyel · Last updated 2026-12-04