1. ## A different sizeof()?

I was wondering if there was a way to makea function, that like the sizeof() function returning the byte number, it returns the bit number?

Code:
```//for example
printf("%d bytes",sizeof(int));

//prints
4 bytes```
Code:
```// I want to make a function modification, or a new function that prints amount of bits
printf("%d bits",sizeofbits(int));  //a new functions

//prints
32 bits```

2. So multliply by CHAR_BITS then.

3. well, I want the function or function modification to be used in special cases when the variable may not be an exact bit number divisible by 8(# of bits in a byte).....
Like say a bit field
Code:
``` struct bitfield{
unsigned first: 1;
unsigned second: 2;
}  struct1;   // this allocates a struct with 3 bits and no bytes
int main()
{
printf("%d", sizeof(struct bitfield));
}
//Would print either a zero or a one depending on machine,which isn't exact```
I want a more exact as the amount of bits allocated in variable
Code:
``` struct bitfield{
unsigned first: 1;
unsigned second: 2;
}  struct1;   // this allocates a variable of a struct with 3 bits and no bytes
int main()
{
printf("%d", sizeofbits(struct1));  // a new function,  or function modification
}
//prints exactly 3 bits```

4. Your belief that your bitfield struct is only three bits long is just that, a belief. It is not necessarily in accordance with reality.

5. So the struct is not exact......?

6. The size of structs in implementation-dependent. I, personally, would be very surprised to come across an implementation that allocated three bits for that struct. I would expect eight bits, with an outside chance of sixteen.

7. So most likely computers always allocates to an sequence of 1 byte or more (which is machine dependent)...
Even on a sega genesis(8-bit machine) I bet

Thanks tabstop, that clears up alot for more, i appreciate it

(I was always wondering why my computer always allocated to 4 bytes, but that was because my machine always located at least to 4 bytes because its 32 bit machine, duh! I feel retarded)

8. It may also help to know that sizeof is not a function, it's a keyword. You don't need the brackets when the argument is a type for example, iirc. There's no run-time penalty at all as there would be if it were a function.

9. Originally Posted by iMalc
You don't need the brackets when the argument is a type for example, iirc.
Actually, you need the parentheses when the operand is a type name.

10. Ah well that shows how often I leave the brackets out! My point is that they can be left out in some cases anyway.