• Home
  • 1.9 Type Modifiers

1.9 Type Modifiers

View Categories

1.9 Type Modifiers

1 min read

Table of Contents

Type modifiers change the size or range of fundamental integer data types. C++ provides the modifiers signed, unsigned, short, and long to represent different ranges of integer values.

This example demonstrates the declaration and use of variables with different type modifiers.

Source Code #

#include <iostream>

int main()
{
    short temperature = -10;
    unsigned int population = 50000;
    long distance = 1500000L;
    long long stars = 20000000000LL;

    std::cout << "Temperature: " << temperature << '\n';
    std::cout << "Population: " << population << '\n';
    std::cout << "Distance: " << distance << '\n';
    std::cout << "Stars: " << stars << '\n';

    return 0;
}

Output #

Temperature: -10
Population: 50000
Distance: 1500000
Stars: 20000000000

Explanation #

  • short stores integer values using fewer bits than int on most implementations, making it suitable for smaller integer ranges.
  • unsigned int stores only non-negative integer values, allowing a larger positive range than a signed int of the same size.
  • long is intended for storing larger integer values than int. Its size is implementation-defined.
  • long long provides an integer type capable of storing larger values than long.
  • The suffix L specifies that the literal is of type long.
  • The suffix LL specifies that the literal is of type long long.
  • The exact size of short, int, long, and long long depends on the compiler and target platform. C++ specifies their minimum ranges rather than fixed sizes.
  • Each std::cout statement prints the value stored in the corresponding variable.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *