CodeBlanks Logo Icon CodeBlanks

Integer types

Learn int, short, long, long long and unsigned types.

Integer types

C++ offers several integer types by size: short (at least 16 bits), int (the basic integer), long (at least 32 bits) and long long (at least 64 bits).

Each type has a signed and unsigned version. signed can hold negative values; unsigned holds only zero and positive but offers twice the range.

The exact size varies by system; for fixed sizes use int32_t, int64_t from the <cstdint> header. For general use int is usually enough.

signed and unsigned

When you write unsigned before a type, it holds only zero and positive values. unsigned int u = 42; is valid; u = -1; gives a compile warning.

signed can hold negative values: signed int s = -5;. By default, types like int and long are signed; writing signed is optional.

Unsigned types have twice the positive range: an 8-bit signed char holds -128..127, unsigned char holds 0..255. For counters that never go negative, unsigned makes sense.

Practice Print three integer types

Inside main, declare an int (value 100), a long (value 1000000) and a long long (value 10000000000). Print each on its own line with std::cout using std::endl.

Starter code

main.cpp
#include <iostream>

int main() {
    // Declare int, long and long long
    // Print each with std::cout
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    // Declare int, long and long long
    int small = 100;
    long medium = 1000000;
    long long large = 10000000000;
    // Print each with std::cout
    std::cout << small << std::endl;
    std::cout << medium << std::endl;
    std::cout << large << std::endl;
    return 0;
}

Expected output

100
1000000
10000000000

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is the largest integer type?

  • short
  • int
  • long long Correct answer
  • long

Explanation: long long is at least a 64-bit integer type.

2 Large integer

Question
____ large = 10000000000;
Answer
long long large = 10000000000;

Explanation: long long holds very large integers.

3 Match types with ranges

Term Meaning
short Small integer
int Basic integer
long long Widest integer
unsigned Positive only

All lessons in Beginner