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 #
shortstores integer values using fewer bits thaninton most implementations, making it suitable for smaller integer ranges.unsigned intstores only non-negative integer values, allowing a larger positive range than a signedintof the same size.longis intended for storing larger integer values thanint. Its size is implementation-defined.long longprovides an integer type capable of storing larger values thanlong.- The suffix
Lspecifies that the literal is of typelong. - The suffix
LLspecifies that the literal is of typelong long. - The exact size of
short,int,long, andlong longdepends on the compiler and target platform. C++ specifies their minimum ranges rather than fixed sizes. - Each
std::coutstatement prints the value stored in the corresponding variable. return 0;terminates the program successfully.