Variables and Fundamental Types

Integer Types

There are several integer types available in C++. These types vary by size and by whether they can represent negative numbers. As a beginner, you just need to know the most common types and a couple of simple rules.

These are the primary integer types you'll see frequently:

#include <iostream>

int main()
{
    short s{10};
    int i{20};
    long l{30};
    long long ll{40};

    std::cout << s << ' '
              << i << ' '
              << l << ' '
              << ll << '\n';
}
  • int: This is the type you should use by default for whole numbers.

  • short: This is smaller and will rarely be needed.

  • long and long long: These are larger. Use them when you know that the numbers are likely to be large.

While the exact size of each of these varies by computer, int should always be large enough for regular counting.

Standard integer types are signed. This means that they can store negative numbers along with positive ones.

int positive{42};
int negative{-7};

You can also declare an unsigned integer type like unsigned int. You should only use this when you know for sure that the numbers in your variable will never be negative.

unsigned int count{0};

For beginners, it is safest to always use the standard signed int. Mixing signed and unsigned variables can be tricky, so if possible, just use normal signed int until you know what you're doing.

#include <iostream>

int main()
{
    int age{25};
    int year{2026};
    int birth_year{year - age};

    std::cout << "You were born around "
              << birth_year
              << '\n';
}

It's good to always declare your variables' type and provide an initial value. Standard integer calculation for normally sized numbers is typically handled by using int.

Exercise: Integer TypesStarter code, hints and solutionOpenClose
Guided practiceBeginner · 10–15 min

Practice: Integer Types

Work through the mission, reveal hints only when needed, and compare your result with the model solution.

Your challenge

Change the short variable s to 11. Predict the four printed values and explain whether changing its value changes its type.

Mission checklist

  1. Run the starter as a baseline, then predict the change requested in Your challenge.
  2. Make that change in your editor; use the lesson-specific hints if you need help.
  3. Compile and run your answer, then compare with the worked solution and explain the result.

Starter program

This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.

#include <iostream>

int main()
{
    short s{10};
    int i{20};
    long l{30};
    long long ll{40};

    std::cout << s << ' '
              << i << ' '
              << l << ' '
              << ll << '\n';
}

Hints

Hint 1

s, i, l, and ll are four distinct integer variables. Their different type names do not alter the order in which the std::cout chain prints them.

Hint 2

Use a small replacement such as 11 for s so it fits even the smallest permitted short range. Do not assume that long has a particular byte size on every system.

Worked solution

s now starts at 11, a value safely representable by short. The other integer variables retain 20, 30, and 40; changing a value does not change the declared type.

#include <iostream>

int main()
{
    short s{11};
    int i{20};
    long l{30};
    long long ll{40};

    std::cout << s << ' '
              << i << ' '
              << l << ' '
              << ll << '\n';
}

Example output

11 20 30 40

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.