Here's a couple of thoughts.I'm learning so there might be mistake(s) and even missing some basics.
sizeof
operator returns number of bytes.number of bits in byte
is not constant value(correct me but it's number of bits char has).- I want to know how many bits variable occupies, and sizeof won't tell me that without making assumptions about number of bits in char.
So I came up with this piece of (probably unnecessary) code:
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main(void)
{
double bits;
bits=sizeof(int)*log10(UCHAR_MAX+1)/log10(2);
printf("Bits = %lf\n", bits);
return 0;
}
Is there easier (standard) way to check how many bits
given type occupies?
CHAR_BIT * sizeof(type)
will do the job, but is there standard one argument
macro/function that does that for me?
Someone with better mathematical background could check if my code will be always giving correct answers.