min and max value of data type in C

后端 未结 9 1613
借酒劲吻你
借酒劲吻你 2020-11-29 17:51

What is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C?

相关标签:
9条回答
  • 2020-11-29 18:45

    The header file limits.h defines macros that expand to various limits and parameters of the standard integer types.

    0 讨论(0)
  • 2020-11-29 18:46

    Look at the these pages on limits.h and float.h, which are included as part of the standard c library.

    0 讨论(0)
  • 2020-11-29 18:53

    Maximum value of any unsigned integral type:

    • ((t)~(t)0) // Generic expression that would work in almost all circumstances.

    • (~(t)0) // If you know your type t have equal or larger size than unsigned int. (This cast forces type promotion.)

    • ((t)~0U) // If you know your type t have smaller size than unsigned int. (This cast demotes type after the unsigned int-type expression ~0U is evaluated.)

    Maximum value of any signed integral type:

    • If you have an unsigned variant of type t, ((t)(((unsigned t)~(unsigned t)0)>>1)) would give you the fastest result you need.

    • Otherwise, use this (thanks to @vinc17 for suggestion): (((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)

    Minimum value of any signed integral type:

    You have to know the signed number representation of your machine. Most machines use 2's complement, and so -(((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)-1 will work for you.

    To detect whether your machine uses 2's complement, detect whether (~(t)0U) and (t)(-1) represent the same thing.

    So, combined with above:

    (-(((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)-(((~(t)0U)==(t)(-1)))
    

    will give you the minimum value of any signed integral type.

    As an example: Maximum value of size_t (a.k.a. the SIZE_MAX macro) can be defined as (~(size_t)0). Linux kernel source code define SIZE_MAX macro this way.

    One caveat though: All of these expressions use either type casting or sizeof operator and so none of these would work in preprocessor conditionals (#if ... #elif ... #endif and like).

    (Answer updated for incorpoating suggestions from @chux and @vinc17. Thank you both.)

    0 讨论(0)
提交回复
热议问题