Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default data type of number in C?

Tags:

c

overflow

In C,

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

which results a warning "integer overflow in expression..." While

unsigned int size = 2147483648;

results no warning?

Is the right value of the first expression is default as int? Where does it mention in C99 spec?

like image 436
mingpepe Avatar asked Jun 03 '16 05:06

mingpepe


People also ask

What is default data type in C with example?

Default data type in c Default data type is opposite of default modifier as you read in the above topic. In case of only SIZE, SIGN, STORAGE CLASS, CONSTANT, and VOLATILE modifier compile automatically assumes int as a default data type.

What are the data types in C language?

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. int: As the name suggests, an int variable is used to store an integer. float: It is used to store decimal numbers (numbers with floating point value) with single precision.

How long is a datatype in C++?

Represented by wchar_t. It is generally 2 or 4 bytes long. As the name implies, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold. Data type modifiers available in C++ are:

What are datatype modifiers in C++?

As the name implies, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold. Data type modifiers available in C++ are:


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 (the current C standard, 6.4.4 Constants p5):

  • int
  • long int
  • long long int

The type of the first expression is int, since every constant with the value 1024 and 2 can be represented as int. The computation of those constants will be done in type int, and the result will overflow.

Assuming INT_MAX equals 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 can be as long int. If INT_MAX equals LONG_MAX equals 2147483647, then the type is long long int.

like image 146
2501 Avatar answered Oct 18 '22 04:10

2501


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

This expression 1024*1024*1024*2 (in the expression 1024 and 2 are of type signed int) produces result that is of type signed int and this value is too big for signed int . Therefore, you get the warning.

After that signed multiplication it is assigned to unsigned int .

like image 28
ameyCU Avatar answered Oct 18 '22 03:10

ameyCU