/* This is a short program that lists the sizes of the basic numeric types. It illustrates some of the options for using fprintf as well as the constants provided in limits.h. To compile: gcc -o sizes 07-sizes.c To run: ./sizes C. Andrews 2013-08-29-2013 */ #include #include /* Our simple main method that prints out the sizes. fprintf (and printf) are _formatted_ output. We use replacement codes within the string, which are then replaced by the items in the comma separated list that follows it. The replacement codes start with a %, and the characters that immediately follow tell fprintf how to interpret the data. This can be a collection of modifiers followed by a final character that provides the type of the output. You can find the full list of codes online, but here are the ones that we use below: Types: d - signed decimal integer u - unsigned decimal integer X - unsigned hexadecimal integer (uppercase) Size modifiers: hh - char/ byte h - short l - long ll - long long Other modifier: # - for octal or hexadecimal, preceed number with O, 0x or 0X as appropriate Examples: %lu - long unsigned decimal %#hhX - one byte upper-case hexadecimal number preceeded by 0X (e.g., 0xF2) The other function in use here is sizeof(), which returns the number of bytes used for storage by the provided type (the input to this function is either a variable or an actual type like int or some structure that you have created). */ int main(int argc, char * argv[]){ printf("char: %lu bytes\n\t%hhd - %hhd [%#hhX - %#hhX]\n", sizeof(char), SCHAR_MIN, SCHAR_MAX, SCHAR_MIN, SCHAR_MAX); printf("unsigned char: %lu bytes\n\t%hhu - %hhu [%#hhX - %#hhX]\n", sizeof(unsigned char), 0, UCHAR_MAX, 0, UCHAR_MAX); printf("short: %lu bytes\n\t%hd - %hd [%#hX - %#hX]\n", sizeof(short), SHRT_MIN, SHRT_MAX, SHRT_MIN, SHRT_MAX); printf("unsigned short: %lu bytes\n\t%hu - %hu [%#hX - %#hX]\n", sizeof(short), 0, USHRT_MAX, 0, USHRT_MAX); printf("int: %lu bytes\n\t%d - %d [%#X - %#X]\n", sizeof(int),INT_MIN, INT_MAX,INT_MIN, INT_MAX); printf("unsigned int: %lu bytes\n\t%u - %u [%#X - %#X]\n", sizeof(int),0, UINT_MAX, 0, UINT_MAX); printf("long: %lu bytes\n\t%ld - %ld [%#lX - %#lX]\n", sizeof(long), LONG_MIN, LONG_MAX, LONG_MIN, LONG_MAX); printf("long: %lu bytes\n\t%lu - %lu [%#lX - %#lX]\n", sizeof(long), 0UL, ULONG_MAX, 0UL, ULONG_MAX); printf("long long: %lu bytes\n\t%lld - %lld [%#llX - %#llX]\n", sizeof(long long), LLONG_MIN, LLONG_MAX, LLONG_MIN, LLONG_MAX); printf("unsigned long long: %lu bytes\n\t%llu - %llu [%#llX - %#llX]\n", sizeof(long long), 0ULL, ULLONG_MAX, 0ULL, ULLONG_MAX); return 0; }