# Integer and the no. of bits it occupy

• 12-15-2005
ramayana
Integer and the no. of bits it occupy
I"m beginning to learn C .The book I follow tells that when any variable has been declared as type int , in 32 bit computer the maximum integer that can be stored is 2147483647 while on 16
bit it is 32767.I'd initialized a variable to 32768 which I could print ,however when I tried to print 2147483647 it gave -1(while I tried to print it on to the screen). So could any one tell me how many bits my compute uses?
• 12-15-2005
Dave_Sinkula
Code:

```#include <stdio.h> #include <limits.h> int main(void) {   printf("INT_MAX = %d\n", INT_MAX);   printf("INT_MIN = %d\n", INT_MIN);   return 0; }```

Code:

```#include <stdio.h> #include <limits.h> int main(void) {   printf("CHAR_BIT = %d\n", CHAR_BIT);   printf("sizeof(int) = %d\n", (int)sizeof(int));   printf("An int occupies %d bits.\n", (int)(CHAR_BIT * sizeof(int)));   return 0; } /* my output CHAR_BIT = 8 sizeof(int) = 4 An int occupies 32 bits. */```
• 12-15-2005
filker0
Or, alternatively:
Code:

```#include <stdio.h> int main(void) {     printf("There are %d bits in an int on this machine\n", sizeof(int) * 8);     return 0; }```
This will give you a direct result for any modern machine where there are 8 bits per smallest addressable memory unit larger than a single bit, which is usually a byte.

It looks as though I was beaten to the punch, and Dave even accounts for different sizes of char. Well done, Dave![/edit]