pereges said:
I wrote a small program to check the range for int(signed) and long
int(signed) on my machine:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("INT_MIN:%d INT_MAX: %d", INT_MIN, INT_MAX);
printf("\nLONG_MAX: %d LONG_MIN: %d", LONG_MIN, LONG_MAX);
return 0;
}
the o/p that i get:
INT_MIN:-2147483648 INT_MAX: 2147483647
LONG_MIN:-2147483648 LONG_MAX: 2147483647
basically the same thing. why is this happening ? also these ranges
seem to contradict with the one given in K & R 2. Does it have
something to do with how the numbers are represented on a particular
machine ?
Also here is a program I wrote some time ago for printing various limits
of the C translation and execution environment.
/* Program to print various implementation defined numerical limits.
*
* Set C99_CONFORMANCE to one if your compiler supports the features
* needed by this program, but fails to define __STDC_VERSION to
199901L.
* TODO: Include complex and imaginary types and features of the runtime
* floating point environment through <fenv.h>
*/
#define C99_CONFORMANCE 1
#if __STDC_VERSION__ == 199901L || C99_CONFORMANCE == 1
#define HAVE_C99 1
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <signal.h>
#include <wchar.h>
#ifdef HAVE_C99
#include <inttypes.h>
#include <stdbool.h>
#endif
int main(void)
{
int dummy;
printf(
"\nCHAR_BIT == %d\n\n"
"Type\t\t\tSize\tMin.\t\t\tMax.\n"
"============================================================\n\n"
#if HAVE_C99 && __bool_true_false_are_defined
"bool\t\t\t%u\n"
#elif HAVE_C99
"_Bool\t\t\t%u\n"
#endif
"char\t\t\t1\t%d\t\t\t%u\n"
"signed char\t\t1\t%d\t\t\t%d\n"
"unsigned char\t\t1\t0\t\t\t%u\n"
"short\t\t\t%u\t%d\t\t\t%d\n"
"unsigned short\t\t%u\t0\t\t\t%u\n"
"int\t\t\t%u\t%d\t\t%d\n"
"unsigned int\t\t%u\t0\t\t\t%u\n"
"long\t\t\t%u\t%ld\t\t%ld\n"
"unsigned long\t\t%u\t0\t\t\t%lu\n",
CHAR_BIT,
#if HAVE_C99 && __bool_true_false_are_defined
(unsigned)sizeof(bool),
#elif HAVE_C99
(unsigned)sizeof(_Bool),
#endif
CHAR_MIN, CHAR_MAX,
SCHAR_MIN, SCHAR_MAX,
UCHAR_MAX,
(unsigned)sizeof(short), SHRT_MIN, SHRT_MAX,
(unsigned)sizeof(unsigned short), USHRT_MAX,
(unsigned)sizeof(int), INT_MIN, INT_MAX,
(unsigned)sizeof(unsigned int), UINT_MAX,
(unsigned)sizeof(long), LONG_MIN, LONG_MAX,
(unsigned)sizeof(unsigned long), ULONG_MAX);
#ifdef HAVE_C99
printf(
"long long\t\t%u\t%lld\t%lld\n"
"unsigned long long\t%u\t0\t\t\t%llu\n",
(unsigned)sizeof(long long), LLONG_MIN, LLONG_MAX,
(unsigned)sizeof(unsigned long long), ULLONG_MAX);
#endif
printf(
"float\t\t\t%u\t%g\t\t%g\n"
"double\t\t\t%u\t%g\t\t%g\n"
"long double\t\t%u\t%Lg\t\t%Lg\n",
(unsigned)sizeof(float), FLT_MIN, FLT_MAX,
(unsigned)sizeof(double), DBL_MIN, DBL_MAX,
(unsigned)sizeof(long double), LDBL_MIN, LDBL_MAX);
printf(
"\nAdditional properties of floating types:\n"
"FLT_RADIX\t\t%d\n"
"FLT_MANT_DIG\t\t%d\n"
"DBL_MANT_DIG\t\t%d\n"
"LDBL_MANT_DIG\t\t%d\n"
#ifdef HAVE_C99
"DECIMAL_DIG\t\t%d\n"
#endif
"FLT_DIG\t\t\t%d\n"
"DBL_DIG\t\t\t%d\n"
"LDBL_DIG\t\t%d\n"
"FLT_MIN_EXP\t\t%d\n"
"FLT_MAX_EXP\t\t%d\n"
"DBL_MIN_EXP\t\t%d\n"
"DBL_MAX_EXP\t\t%d\n"
"LDBL_MIN_EXP\t\t%d\n"
"LDBL_MAX_EXP\t\t%d\n"
"FLT_MIN_10_EXP\t\t%d\n"
"FLT_MAX_10_EXP\t\t%d\n"
"DBL_MIN_10_EXP\t\t%d\n"
"DBL_MAX_10_EXP\t\t%d\n"
"LDBL_MIN_10_EXP\t\t%d\n"
"LDBL_MAX_10_EXP\t\t%d\n"
"FLT_EPSILON\t\t%g\n"
"DBL_EPSILON\t\t%g\n"
"LDBL_EPSILON\t\t%Lg\n",
FLT_RADIX, FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG,
#ifdef HAVE_C99
DECIMAL_DIG,
#endif
FLT_DIG, DBL_DIG, LDBL_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
DBL_MIN_EXP,
DBL_MAX_EXP, LDBL_MIN_EXP, LDBL_MAX_EXP, FLT_MIN_10_EXP,
FLT_MAX_10_EXP, DBL_MIN_10_EXP, DBL_MAX_10_EXP, LDBL_MIN_10_EXP,
LDBL_MAX_10_EXP, FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON);
#ifdef HAVE_C99
printf(
"\nProperties of types defined in stdint.h\n"
#ifdef INT8_MIN
"int8_t\t\t\t%u\t%" PRId8 "\t\t\t%" PRId8 "\n"
"uint8_t\t\t\t%u\t0\t\t\t%" PRIu8 "\n"
#endif
#ifdef INT16_MIN
"int16_t\t\t\t%u\t%" PRId16 "\t\t\t%" PRId16 "\n"
"uint16_t\t\t%u\t0\t\t\t%" PRIu16 "\n"
#endif
#ifdef INT32_MIN
"int32_t\t\t\t%u\t%" PRId32 "\t\t%" PRId32 "\n"
"uint32_t\t\t%u\t0\t\t\t%" PRIu32 "\n"
#endif
#ifdef INT64_MIN
"int64_t\t\t\t%u\t%" PRId64 "\t%" PRId64 "\n"
"uint64_t\t\t%u\t0\t\t\t%" PRIu64 "\n"
#endif
"int_least8_t\t\t%u\t%" PRIdLEAST8 "\t\t\t%" PRIdLEAST8 "\n"
"uint_least8_t\t\t%u\t0\t\t\t%" PRIuLEAST8 "\n"
"int_least16_t\t\t%u\t%" PRIdLEAST16 "\t\t\t%" PRIdLEAST16 "\n"
"uint_least16_t\t\t%u\t0\t\t\t%" PRIuLEAST16 "\n"
"int_least32_t\t\t%u\t%" PRIdLEAST32 "\t\t%" PRIdLEAST32 "\n"
"uint_least32_t\t\t%u\t0\t\t\t%" PRIuLEAST32 "\n"
"int_least64_t\t\t%u\t%" PRIdLEAST64 "\t%" PRIdLEAST64 "\n"
"uint_least64_t\t\t%u\t0\t\t\t%" PRIuLEAST64 "\n"
"int_fast8_t\t\t%u\t%" PRIdFAST8 "\t\t\t%" PRIdFAST8 "\n"
"uint_fast8_t\t\t%u\t0\t\t\t%" PRIuFAST8 "\n"
"int_fast16_t\t\t%u\t%" PRIdFAST16 "\t\t%" PRIdFAST16 "\n"
"uint_fast16_t\t\t%u\t0\t\t\t%" PRIuFAST16 "\n"
"int_fast32_t\t\t%u\t%" PRIdFAST32 "\t\t%" PRIdFAST32 "\n"
"uint_fast32_t\t\t%u\t0\t\t\t%" PRIuFAST32 "\n"
"int_fast64_t\t\t%u\t%" PRIdFAST64 "\t%" PRIdFAST64 "\n"
"uint_fast64_t\t\t%u\t0\t\t\t%" PRIuFAST64 "\n"
"intmax_t\t\t%u\t%" PRIdMAX "\t%" PRIdMAX "\n"
"uintmax_t\t\t%u\t0\t\t\t%" PRIuMAX "\n"
#ifdef INTPTR_MIN
"intptr_t\t\t%u\t%" PRIdPTR "\t\t%" PRIdPTR "\n"
#endif
#ifdef UINTPTR_MAX
"uintptr_t\t\t%u\t0\t\t\t%" PRIuPTR "\n"
#endif
"%n"
,
#ifdef INT8_MIN
(unsigned)sizeof(int8_t), INT8_MIN, INT8_MAX,
(unsigned)sizeof(uint8_t), UINT8_MAX,
#endif
#ifdef INT16_MIN
(unsigned)sizeof(int16_t), INT16_MIN, INT16_MAX,
(unsigned)sizeof(uint16_t), UINT16_MAX,
#endif
#ifdef INT32_MIN
(unsigned)sizeof(int32_t), INT32_MIN, INT32_MAX,
(unsigned)sizeof(uint32_t), UINT32_MAX,
#endif
#ifdef INT64_MIN
(unsigned)sizeof(int64_t), INT64_MIN, INT64_MAX,
(unsigned)sizeof(uint64_t), UINT64_MAX,
#endif
(unsigned)sizeof(int_least8_t), INT_LEAST8_MIN, INT_LEAST8_MAX,
(unsigned)sizeof(uint_least8_t), UINT_LEAST8_MAX,
(unsigned)sizeof(int_least16_t), INT_LEAST16_MIN,
INT_LEAST16_MAX,
(unsigned)sizeof(uint_least16_t), UINT_LEAST16_MAX,
(unsigned)sizeof(int_least32_t), INT_LEAST32_MIN,
INT_LEAST32_MAX,
(unsigned)sizeof(uint_least32_t), UINT_LEAST32_MAX,
(unsigned)sizeof(int_least64_t), INT_LEAST64_MIN,
INT_LEAST64_MAX,
(unsigned)sizeof(uint_least64_t), UINT_LEAST64_MAX,
(unsigned)sizeof(int_fast8_t), INT_FAST8_MIN, INT_FAST8_MAX,
(unsigned)sizeof(uint_fast8_t), UINT_FAST8_MAX,
(unsigned)sizeof(int_fast16_t), INT_FAST16_MIN, INT_FAST16_MAX,
(unsigned)sizeof(uint_fast16_t), UINT_FAST16_MAX,
(unsigned)sizeof(int_fast32_t), INT_FAST32_MIN, INT_FAST32_MAX,
(unsigned)sizeof(uint_fast32_t), UINT_FAST32_MAX,
(unsigned)sizeof(int_fast64_t), INT_FAST64_MIN, INT_FAST64_MAX,
(unsigned)sizeof(uint_fast64_t), UINT_FAST64_MAX,
(unsigned)sizeof(intmax_t), INTMAX_MIN, INTMAX_MAX,
(unsigned)sizeof(uintmax_t), UINTMAX_MAX,
#ifdef INTPTR_MIN
(unsigned)sizeof(intptr_t), INTPTR_MIN, INTPTR_MAX,
#endif
#ifdef UINTPTR_MAX
(unsigned)sizeof(uintptr_t), UINTPTR_MAX,
#endif
&dummy);
#endif
printf(
"\nOther types:\n"
"ptrdiff_t\t\t%u"
#ifdef PTRDIFF_MIN
"\t%td\t\t%td"
#endif
"\n"
"size_t\t\t\t%u\t0\t\t\t%lu\n"
#ifdef WCHAR_MIN
"wchar_t\t\t\t%u\t%ld\t\t%lu\n"
#endif
#ifdef WINT_MIN
"wint_t\t\t\t%u\t%d\t\t\t%u\n"
#endif
"sig_atomic_t\t\t%u"
#ifdef SIG_ATOMIC_MIN
"\t%d\t\t%u"
#endif
"\n"
"BUFSIZ\t\t\t%lu\n"
"FOPEN_MAX\t\t%u\n"
"FILENAME_MAX\t\t%u\n"
"L_tmpnam\t\t%u\n"
"TMP_MAX\t\t\t%lu\n"
"RAND_MAX\t\t%lu\n"
"MB_LEN_MAX\t\t%lu\n"
"MB_CUR_MAX\t\t%lu\n"
"%n",
(unsigned)sizeof(ptrdiff_t),
#ifdef PTRDIFF_MIN
PTRDIFF_MIN, PTRDIFF_MAX,
#endif
(unsigned)sizeof(size_t), (unsigned long)((size_t)-1),
#ifdef WCHAR_MIN
(unsigned)sizeof(wchar_t), WCHAR_MIN, WCHAR_MAX,
#endif
#ifdef WINT_MIN
(unsigned)sizeof(wint_t), WINT_MIN, WINT_MAX,
#endif
(unsigned)sizeof(sig_atomic_t),
#ifdef SIG_ATOMIC_MIN
SIG_ATOMIC_MIN, SIG_ATOMIC_MAX,
#endif
(unsigned long)BUFSIZ, FOPEN_MAX, FILENAME_MAX, L_tmpnam,
(unsigned long)TMP_MAX, (unsigned long)RAND_MAX,
(unsigned long)MB_LEN_MAX, (unsigned long)MB_CUR_MAX, &dummy);
return 0;
}