Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    C++ fundamentals · Lesson 2 of 21

    Variables and types

    int, double, char, bool, string, auto and const, and why types matter.

    9 minutes3 games

    In C++ you state each variable's type when you declare it, and it never changes. That's static typing: the compiler knows everything before the program runs, catches many mistakes early, and reserves exactly the right amount of memory.

    C++
    #include <iostream>
    #include <string>
    
    int main() {
        int age = 20;
        double gpa = 3.75;
        char grade = 'A';
        bool enrolled = true;
        std::string name = "Laila";
        const int maxCourses = 5;
        auto score = 98.5;          // the compiler works out: double
    
        std::cout << name << " is " << age << ", grade " << grade << "\n";
        std::cout << "GPA " << gpa << ", enrolled: " << enrolled << "\n";
        std::cout << "max " << maxCourses << ", score " << score << "\n";
        return 0;
    }
    age20int · 4 bytes
    gpa3.75double · 8 bytes
    grade'A'char · 1 byte
    enrolledtruebool · 1 byte
    name"Laila"std::string

    Each type takes a known amount of space. (These are the usual sizes; the standard only sets minimums.)

    Notice bool printed 1, not true. A char uses single quotes 'A', a string double quotes "Laila". const means the value never changes; try to change it and the compiler refuses.

    The division and overflow traps

    C++
    #include <iostream>
    #include <climits>
    
    int main() {
        std::cout << 7 / 2 << "\n";        // int / int  -> int
        std::cout << 7 / 2.0 << "\n";      // one double -> double
    
        int big = INT_MAX;
        std::cout << big << "\n";
        long long bigger = static_cast<long long>(big) + 1;
        std::cout << bigger << "\n";
        return 0;
    }

    Important

    Dividing an int by an int drops the fraction (7 / 2 is 3). If an int goes past its maximum (about 2.1 billion) that's overflow, and for signed numbers it's undefined behavior: the program may do anything. For big numbers use long long.

    What's the output?

    #include <iostream>
    int main() {
        int a = 9, b = 4;
        double c = a / b;
        std::cout << c << "\n";
    }

    What will this code print?

    Complete the code

    Complete it so the division gives 2.25.

    #include <iostream>
    int main() {
        int a = 9, b = 4;
        double c = <double>(a) / b;
        std::cout << c << "\n";
    }

    Interview question

    What is undefined behavior in C++? Give examples.

    Exercise

    You have int total = 17 and int count = 4. Print the average with its fraction (4.25).

    Expected output: 4.25

    C++
    #include <iostream>
    
    int main() {
        int total = 17;
        int count = 4;
        std::cout << total / count << "\n";
        return 0;
    }

    Learn more from

    • learncpp: Fundamental data typeslearncpp.com
    • cppreference: Fundamental typesen.cppreference.com
    • cppreference: Undefined behavioren.cppreference.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousYour first C++ programNextOperators and input
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.