Thread: Determining the size of an int on current computer

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2012
Posts
28
Rep Power
0

Determining the size of an int on current computer

Trying to determine the size of an int on my pc. Compiling the following program using dev c++. Any idea where I'm going wrong? Program keeps telling me an int is zero bits long! :o

Thanks!

Code:
```// program to determine the size of an int on the current computer

#include <stdio.h>

int main (void)
{
// initialise an int containing all ones in binary
int test_int = ~0; // complement of all zeroes (i.e. all ones)

int bit_count = 0;

while (test_int >= 0)
{
test_int /= 2;
bit_count++;
}
printf("There are %i bits in an int on this computer.", bit_count);

return 0;
}```
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2012
Posts
83
Rep Power
39
~0 as a signed integer is a negative number. As such the condition test_int >= 0 will be false from the beginning and the loop will never run. If you changed test_int to be an unsigned integer, the loop would run forever (because a number can't become less than 0 through division - and an unsigned number can't become less than zero through anything). If you also changed the while condition to be > 0 rather than >= 0, it would do what you want.

That said the proper way to find out the size of an int is to do sizeof(int), which returns the number of bytes in an int. To get the number of bits multiply by CHAR_BITS.
3. You are overcomplicating a trivial thing - by definition, the compiler already knows the length of an int:

Code:
`    int bit_count = sizeof(int) * CHAR_BIT ;`
CHAR_BIT is a macro defined in the standard header <limits.h>.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
101
Rep Power
0
To determine the size:

Code:
```#include<stdio.h>

int main()
{
printf("%i",sizeof(int));
getchar();
return 0;
}```
To determine the maximum size an int (not unsigned int) can hold:

Code:
```#include<stdio.h>
#include<limits.h> //Library is necessary

int main()
{
printf("%d is the maximum length for an int",INT_MAX);
getchar();
return 0;
}```
5. Originally Posted by miz6565
Code:
`	printf("%i",sizeof(int));`
The OP is attempting to determine the size in bits. Multiply by CHAR_BITS (in limits.h) required. sizeof() yields teh size in multiples of char sized words. I regularly work with a DSP platform where that is 16 bits, so it is not always 8.

Originally Posted by miz6565
Code:
`#include<limits.h> //Library is necessary`
limits.h is a header only, no library code is required, and a header and a library are not the same thing.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2012
Posts
28
Rep Power
0
Thanks very much for all your help. I've almost finished reading Programming in C by Stephen Kochan, but I've still got quite a few of the end of chapter exercises to complete. Dev Shed certainly makes doing this a lot easier! :)