# Thread: different size of integer

1. ## different size of integer

Integer type come with different size such as signed, unsigned, short, long, double

Is it possible to find out size of integer because I don't see any way.
Code:
```#include<stdio.h>
int main (void)
{
int x = 5;
signed int y = 20;
unsigned int z = 4;

long int a = 10;
long unsigned int b = 20;
long signed int c = 30 ;

short int k = 8;
short unsigned int l = 12;
short signed int m = 16;

printf(" print value of x %d \n",x);
printf(" print value of y %d \n",y);
printf(" print value of z %d \n \n",z);

printf(" print value of a %d \n",a);
printf(" print value of b %d \n",b);
printf(" print value of c %d \n \n",c);

printf(" print value of k %d \n",k);
printf(" print value of l %d \n",l);
printf(" print value of m %d \n \n",m);

return 0;
}```
print value of x 5
print value of y 20
print value of z 4
print value of a 10
print value of b 20
print value of c 30
print value of k 8
print value of l 12
print value of m 16 2. Use sizeof, e.g.,
Code:
`printf("size of int: %u\n", (unsigned int)sizeof(int));`
or if you are compiling with respect to C99 or later:
Code:
`printf("size of int: %zu\n", sizeof(int));`
By the way, double is not an integer type. 3. Also, what may be far more important is the range of a given type. There are constants for them defined in limits.h if you actually need them for programming reasons. If you just want to know, say, the largest number an integer type can hold, do some math and you will have a reasonable guess, and a guess that will always be true on two's compliment systems.

The maximum value an integer type x can hold is 2^(sizeof(x)*CHAR_BIT) if it is unsigned. Subtract 1 from that if the type is signed.

CHAR_BIT is the number of bits to a byte. So, the maximum of a long may be 2^(4*8) - 1, also known as 2^31, also known as 2,147,483,648. 4. but compiler show same size for every types
Code:
```#include<stdio.h>
int main (void)
{
int x = 5;
signed int y = 20;
unsigned int z = 4;

long int a = 10;
unsigned long int b = 20;
signed long int c = 30 ;

short int k = 8;
unsigned short int l = 12;
signed short int m = 16;

printf("size of signed int : %u \n", (signed int)sizeof(int));
printf("size of unsigned int : %u \n", (unsigned int)sizeof(int));

printf("size of long int : %ld \n", (long int)sizeof(int));
printf("size of signed long int : %u \n", (signed long int)sizeof(int));
printf("size of unsigned long int : %lu \n", (unsigned long int)sizeof(int));

printf("size of short int : %hd \n", (short int)sizeof(int));
printf("size of signed short int : %u \n", (signed short int)sizeof(int));
printf("size of unsigned short int : %hu \n", (unsigned short int)sizeof(int));

return 0;
}```
size of signed int : 4
size of unsigned int : 4
size of long int : 4
size of signed long int : 4
size of unsigned long int : 4
size of short int : 4
size of signed short int : 4
size of unsigned short int : 4 5. Originally Posted by vead
but compiler show same size for every types
No, compiler is showing the same result for sizeof(int) no matter how many times you use it. Obviously, this is your typo error, not the compiler's fault. 6. Originally Posted by laserlight No, compiler is showing the same result for sizeof(int) no matter how many times you use it. Obviously, this is your typo error, not the compiler's fault.
Program
Code:
```#include<stdio.h>
int main (void)
{
int x = 5;
signed int y = 20;
unsigned int z = 4;

long int a = 10;
unsigned long int b = 20;
signed long int c = 30 ;

short int k = 8;
unsigned short int l = 12;
signed short int m = 16;

printf("size of signed int : %d \n", (signed int)sizeof(signed int));
printf("size of unsigned int : %u \n", (unsigned int)sizeof(unsigned int));

printf("size of long int : %li \n", (long int)sizeof(long int));
printf("size of signed long int : %li \n", (signed long int)sizeof(signed long int));
printf("size of unsigned long int : %lu \n", (unsigned long int)sizeof(unsigned long int));

printf("size of short int : %hi \n", (short int)sizeof(short int));
printf("size of signed short int : %hi \n", (signed short int)sizeof(signed short int));
printf("size of unsigned short int : %hu \n", (unsigned short int)sizeof(unsigned short int));

return 0;
}```
size of signed int : 4 //does it means it can be store 4 bytes data

size of unsigned int : 4
size of long int : 4
size of signed long int : 4
size of unsigned long int : 4

size of short int : 2
size of signed short int : 2 //does it means it can be store 2 bytes data
size of unsigned short int : 2 7. Why don't you use the Fixed width integer types, then you know exctly the size of your types.
Fixed width integer types (since C99) - cppreference.com Popular pages Recent additions int, print, printf, short, unsigned 