Last active
June 24, 2025 21:20
-
-
Save DocBohn/44ff1b860a92a7120795cabc1f7002a6 to your computer and use it in GitHub Desktop.
Prints the number of bits used by, and the number of bytes allocated for, the standard types, or "-1" if unknown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
#include <stdint.h> // for PTRDIFF_WIDTH | |
#include <stdbool.h> // for pre-C23 | |
#include <float.h> | |
#include <math.h> | |
#include <uchar.h> | |
#if !defined (BOOL_WIDTH) | |
#define BOOL_WIDTH (-1) | |
#endif | |
#if !defined (CHAR_WIDTH) | |
#define CHAR_WIDTH (CHAR_BIT) | |
#endif | |
#if !defined (SHRT_WIDTH) | |
#define SHRT_WIDTH (-1) | |
#endif | |
#if !defined (INT_WIDTH) | |
#define INT_WIDTH (-1) | |
#endif | |
#if !defined (LONG_WIDTH) | |
#define LONG_WIDTH (-1) | |
#endif | |
#if !defined (LLONG_WIDTH) | |
#define LLONG_WIDTH (-1) | |
#endif | |
#if !defined (UINTPTR_WIDTH) | |
#define UINTPTR_WIDTH (-1) | |
#endif | |
_Float16 bar(); | |
_Float16 baz(); | |
int main() { | |
printf(" %-10s%-10s\n", "Bits", "Bytes"); | |
printf("%-12s%-10s%-10s\n", "Type", "Used", "Allocated"); | |
printf("--------------------------------\n"); | |
printf("%-12s%3d %2zu\n", "bool", BOOL_WIDTH, sizeof(bool)); | |
printf("%-12s%3d %2zu\n", "char", CHAR_WIDTH, sizeof(char)); | |
printf("%-12s%3d %2zu\n", "short", SHRT_WIDTH, sizeof(short)); | |
printf("%-12s%3d %2zu\n", "int", INT_WIDTH, sizeof(int)); | |
printf("%-12s%3d %2zu\n", "long", LONG_WIDTH, sizeof(long)); | |
printf("%-12s%3d %2zu\n", "long long", LLONG_WIDTH, sizeof(long long)); | |
printf("%-12s%3d %2zu\n", "void *", UINTPTR_WIDTH, sizeof(void *)); | |
// float and double don't encode the implicit leading 0/1, but it is counted in xxx_MANT_DIG | |
// log2(2 * FLT_MAX_EXP) gets us the number of bits for the exponent | |
// add 1 for the sign bit | |
printf("%-12s%3d %2zu\n", "float", (FLT_MANT_DIG - 1) + (int)log2(2 * FLT_MAX_EXP) + 1, sizeof(float)); | |
printf("%-12s%3d %2zu\n", "double", (DBL_MANT_DIG - 1) + (int)log2(2 * DBL_MAX_EXP) + 1, sizeof(double)); | |
#if defined (__x86_64__) || defined (_M_X64) || defined (i386) || defined (__i386__) || defined (__i386) || defined (_M_IX86) || defined (_X86_) | |
// x87 long double *does* encode the explicit leading 0/1 | |
printf("%-12s%3d %2zu\n", "long double", (LDBL_MANT_DIG) + (int)log2(2 * LDBL_MAX_EXP) + 1, sizeof(long double)); | |
#else | |
printf("%-12s%3d %2zu\n", "long double", (LDBL_MANT_DIG - 1) + (int)log2(2 * LDBL_MAX_EXP) + 1, sizeof(long double)); | |
#endif | |
// these are C23-specific | |
// printf("%-12s%3d %2zu\n", "_Float16", -1, sizeof(_Float16)); | |
// printf("%-12s%3d %2zu\n", "_Float128", -1, sizeof(_Float128)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment