Reputation: 1047
I need to know the width(size) of a native int in the system which my code is being run, in bits.
I can get the width in chars using "sizeof", however, it is my understanding that no-where in the C spec does a char have a specific number of bits assigned to it.
So what is the best cross platform method for finding the bit width of an int?
Upvotes: 2
Views: 960
Reputation: 8861
Open up limits.h
and you should see all the info you need about sizes on your particular platform's integer limits.
This is how you determine how many bits in an int
:
#include <limits.h>
printf("Number of bits in an int: %zu\n", sizeof(int) * CHAR_BIT);
Open up float.h
if you're interested in your particular platform's float
and double
limits.
Upvotes: 3
Reputation: 539765
CHAR_BIT
(from <limits.h>
) is the number of bits in a char
, so
sizeof(int) * CHAR_BIT
is what you are looking for.
CHAR_BIT
is specified in "5.2.4.2.1 Sizes of integer types "
of the C99 standard.
Added: It might be interesting that the Open Group specification
states:
The values for the limits {CHAR_BIT}, {SCHAR_MAX}, and {UCHAR_MAX} are now required to be 8, +127, and 255, respectively.
Upvotes: 8
Reputation: 832
I would be very amazed if anything didn't use 8 bits per character. You could fill a char with more then it should hold like 0xFFFF and see what it's value is.
Upvotes: 0