Variables declared with int data type can accommodate 32-Bit integer value i.e. 4BYTES.
- The size varies from compiler to compiler i.e. in 32-bit TCC and BCC compiler the size of int is 2 BYTES. Whereas in the GCC compiler it is of 4BYTES.
- A few type modifiers such as short, long, signed, and unsigned impact the Size and range of the integer data type.
Signed integer varies from -2,147,483,648 to +2,147,483,647 i.e. 31 bits for data and the most significant bit will represent sign (+/-).
Unsigned integer varies from 0 to 4,294,967,295. All 32 bits represent data.
Declaring and defining an integer variable:
We need to mention the keyword int before the variable name to specify the variable is of type int and can store an only integer value.
#include <stdio.h>
int main()
{
/* <datatype> <variable 1>,<variable 2>,<variable 3>...... <variable N>; */
/* Declaring an integer variable */
int value;
/* Defining value to a variable */
value = 30;
/* printing the value on to the screen */
printf("The integer value is %d", value);
return 0;
}
- %d is the format specifier to represent the variable as of type int.
- ‘value’ is a variable name.
Initializing an integer variable:
Declaration + Definition = Initialization;
#include <stdio.h>
int main()
{
/* Declaring and defining (Initializing)
the value at same time */
int value = 30;
printf("The value is %d", value);
return 0;
}
Binary representation:
The below binary representation will show, how the value 30 will be stored in memory.
- The default type modifier of an integer variable is ‘signed’.
- The integer variable can hold only one integer value at a time.
Wrap-up Behaviour of integer:
If the assigned value is not in the range of the data type then the value will be wrapped up.
For example:
- If we initialize the variable with a value
2,147,483,648
that is not in the range of integer, so it will be wrapped up to the opposite value in the range i.e-2,147,483,648
. Similarly for2,147,483,649
will be wrapped up to-2,147,483,647
. - For an unsigned integer,
4,294,967,296
will be wrapped up to0
.
Leave a Comment