Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What range of values can integer types store in C++

Tags:

c++

int

Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer?

Additionally, what are the ranges of unsigned long int , long int, unsigned int, short int, short unsigned int, and int?

like image 792
yihangho Avatar asked Nov 30 '09 11:11

yihangho


People also ask

What is the range of integer type?

The INTEGER data type stores whole numbers that range from -2,147,483,647 to 2,147,483,647 for 9 or 10 digits of precision.

What is the range of values that can be stored by int data type in C Mcq?

What is the range of values that can be stored by int datatype in C? The range of int is from -(2^31) to (2^31) - 1.


1 Answers

The minimum ranges you can rely on are:

  • short int and int: -32,767 to 32,767
  • unsigned short int and unsigned int: 0 to 65,535
  • long int: -2,147,483,647 to 2,147,483,647
  • unsigned long int: 0 to 4,294,967,295

This means that no, long int cannot be relied upon to store any 10 digit number. However, a larger type long long int was introduced to C in C99 and C++ in C++11 (this type is also often supported as an extension by compilers built for older standards that did not include it). The minimum range for this type, if your compiler supports it, is:

  • long long int: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
  • unsigned long long int: 0 to 18,446,744,073,709,551,615

So that type will be big enough (again, if you have it available).


A note for those who believe I've made a mistake with these lower bounds: the C requirements for the ranges are written to allow for ones' complement or sign-magnitude integer representations, where the lowest representable value and the highest representable value differ only in sign. It is also allowed to have a two's complement representation where the value with sign bit 1 and all value bits 0 is a trap representation rather than a legal value. In other words, int is not required to be able to represent the value -32,768.

like image 134
caf Avatar answered Oct 20 '22 08:10

caf