Skip to content

Instantly share code, notes, and snippets.

@jwodder
Created May 17, 2014 00:28
Show Gist options
  • Save jwodder/4008289fe808afd76284 to your computer and use it in GitHub Desktop.
Save jwodder/4008289fe808afd76284 to your computer and use it in GitHub Desktop.
Print various values from <limits.h> and <float.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>
void showInt(const char* name, size_t bytes, intmax_t max, intmax_t min,
uintmax_t umax);
/* Converting between floating-point types undoubtedly won't preserve things
* appropriately, so showFloat is implemented as a macro: */
#define showFloat(name, form, bytes, sig, dig, epsilon, min, max, minExp, \
maxExp, min10exp, max10exp) \
printf(name ":\n Bytes: %zu\t\t\t\t Significand length: %d\n", bytes, sig); \
printf(" Decimal precision: %-16d\t Epsilon: %.*" form "\n", dig, dig, \
epsilon); \
printf(" Minimum: %-30.*" form " Min radix exponent: %d\n", dig, min, \
minExp); \
printf(" Maximum: %-30.*" form " Max radix exponent: %d\n", dig, max, \
maxExp); \
printf(" Min base 10 exponent: %-16d\t Max base 10 exponent: %d\n\n", \
min10exp, max10exp);
int main(void) {
#ifdef __STDC_VERSION__
printf("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
#else
puts("!defined(__STDC_VERSION__)");
#endif
printf("sizeof(void*): %zu\n", sizeof(void*));
printf("Floating-point radix: %d\n", FLT_RADIX);
#ifdef MB_LEN_MAX
printf("Max bytes in a multibyte char: %d\n", MB_LEN_MAX);
#else
puts("!defined(MB_LEN_MAX)");
#endif
putchar('\n');
printf("char:\n Bits: %-16d\t\t\t Bytes: %zu\n", CHAR_BIT, sizeof(char));
printf(" Minimum: %-16d\t\t Signed min: %d\n", CHAR_MIN, SCHAR_MIN);
printf(" Maximum: %-16d\t\t Signed max: %d\n", CHAR_MAX, SCHAR_MAX);
printf(" Unsigned max: %d\n\n", UCHAR_MAX);
showInt("short int", sizeof(short int), SHRT_MAX, SHRT_MIN, USHRT_MAX);
showInt("int", sizeof(int), INT_MAX, INT_MIN, UINT_MAX);
showInt("long int", sizeof(long int), LONG_MAX, LONG_MIN, ULONG_MAX);
#if __STDC_VERSION__ >= 199901L
showInt("long long int", sizeof(long long int), LLONG_MAX, LLONG_MIN,
ULLONG_MAX);
#endif
/*
#include <stddef.h> // ptrdiff_t, size_t, wchar_t
#include <signal.h> // sig_atomic_t
#include <wchar.h> // wchar_t, wint_t
showInt("[u]intptr_t", sizeof(intptr_t), INTPTR_MAX, INTPTR_MIN, UINTPTR_MAX);
showInt("[u]intmax_t", sizeof(intmax_t), INTMAX_MAX, INTMAX_MIN, UINTMAX_MAX);
showInt("ptrdiff_t", sizeof(ptrdiff_t), PTRDIFF_MAX, PTRDIFF_MIN, 0);
showInt("sig_atomic_t", sizeof(sig_atomic_t), SIG_ATOMIC_MAX, SIG_ATOMIC_MIN,
0);
printf("size_t:\n Bytes: %zu\t\t\t\t Maximum: %zu\n\n", sizeof(size_t),
SIZE_MAX);
showInt("wchar_t", sizeof(wchar_t), WCHAR_MAX, WCHAR_MIN, 0);
showInt("wint_t", sizeof(wint_t), WINT_MAX, WINT_MIN, 0);
*/
showFloat("float", "g", sizeof(float), FLT_MANT_DIG, FLT_DIG, FLT_EPSILON,
FLT_MIN, FLT_MAX, FLT_MIN_EXP, FLT_MAX_EXP, FLT_MIN_10_EXP, FLT_MAX_10_EXP);
showFloat("double", "g", sizeof(double), DBL_MANT_DIG, DBL_DIG, DBL_EPSILON,
DBL_MIN, DBL_MAX, DBL_MIN_EXP, DBL_MAX_EXP, DBL_MIN_10_EXP, DBL_MAX_10_EXP);
showFloat("long double", "Lg", sizeof(long double), LDBL_MANT_DIG, LDBL_DIG,
LDBL_EPSILON, LDBL_MIN, LDBL_MAX, LDBL_MIN_EXP, LDBL_MAX_EXP,
LDBL_MIN_10_EXP, LDBL_MAX_10_EXP);
return 0;
}
void showInt(const char* name, size_t bytes, intmax_t max, intmax_t min,
uintmax_t umax) {
printf("%s:\n", name);
printf(" Bytes: %zu", bytes);
if (umax != 0) printf("\t\t\t\t Unsigned max: %ju", umax);
printf("\n Minimum: %-30jd Maximum: %jd\n\n", min, max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment