> Write a C program that will display storage required in bits and bytes by all the intrinsic types permitted in C?

Write a C program that will display storage required in bits and bytes by all the intrinsic types permitted in C?

Posted at: 2014-12-18 
You could try something like the following:

#include

#define INFO( a ) #a, sizeof(a), sizeof(a)*8

#define FMT "%30s\tBytes = %u, Bits = %u\n"

int main( void ) {

? ? printf( FMT, INFO( char ) );

? ? printf( FMT, INFO( int ) );

? ? printf( FMT, INFO( long ) );

? ? printf( FMT, INFO( short ) );

? ? printf( FMT, INFO( long long ) );

? ? printf( FMT, INFO( unsigned int ) );

? ? printf( FMT, INFO( unsigned long ) );

? ? printf( FMT, INFO( unsigned short ) );

? ? printf( FMT, INFO( unsigned char ) );

? ? printf( FMT, INFO( unsigned long long ) );

? ? printf( FMT, INFO( char * ) );

? ? printf( FMT, INFO( int * ) );

? ? printf( FMT, INFO( long * ) );

? ? printf( FMT, INFO( short * ) );

? ? printf( FMT, INFO( long long * ) );

? ? printf( FMT, INFO( unsigned int * ) );

? ? printf( FMT, INFO( unsigned long * ) );

? ? printf( FMT, INFO( unsigned short * ) );

? ? printf( FMT, INFO( unsigned char * ) );

? ? printf( FMT, INFO( unsigned long long * ) );

? ? printf( FMT, INFO( double ) );

? ? printf( FMT, INFO( float ) );

? ? printf( FMT, INFO( double * ) );

? ? printf( FMT, INFO( float * ) );

? ? printf( FMT, INFO( void * ) );

? ? printf( FMT, INFO( void (*)( void ) ) );

? ? return 0;

}

If you want to add more intrinsic types, feel free.

EDIT (to Roger): I'm pretty sure CHAR_BIT = 8 on all conforming implementations. (Yes, I still remember the PDP-10. But long before standards for C.) The standard says:

"The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."

Note that it says "except for" and lists CHAR_BIT. The next few lines of the standard set CHAR_BIT to 8.

Do you know of a single standards-compliant C compiler that doesn't set CHAR_BIT to 8? I'd be interested.

Jonathan is almost right but you should use CHAR_BIT instead of 8.

Not all systems use 8 bit bytes

#include

#define INFO( a ) #a, sizeof(a), sizeof(a)*CHAR_BIT

http://www.gnu.org/software/libc/manual/...