What is the default data type in C?

In C,

unsigned int size = 1024*1024*1024*2; 

as a result of which the warning โ€œoverflow of integers in the expression ...โ€ appears while

 unsigned int size = 2147483648; 

no warning?

Is the value of the first expression used correctly as an int? Where is this mentioned in the C99 specification?

+7
c overflow
source share
2 answers

When using a decimal constant without any suffixes, the type of the decimal constant is the first that can be represented, in order (current standard C, 6.4.4 Constants p5):

  • INT
  • long int
  • long long int

The type of the first expression is int , since each constant with values โ€‹โ€‹of 1024 and 2 can be represented as int. The calculation of these constants will be performed in the int type, and the result will overflow.

Assuming that INT_MAX is 2147483647 and LONG_MAX is greater than 2147483647, the type of the second expression is long int , since this value cannot be represented as int, but it can be the same long int. If INT_MAX is LONG_MAX is 2147483647, then the type is long long int .

+9
source share
 unsigned int size = 1024*1024*1024*2; 

This expression 1024*1024*1024*2 (in the expression 1024 and 2 is of type signed int ) gives a result of type signed int , and this value is too large for signed int . Therefore, you get a warning.

After this signed multiplication, an unsigned int is assigned.

+3
source share

All Articles