C++ maximum non negative int

前端 未结 5 1454
既然无缘
既然无缘 2021-01-28 12:24

Is the following going to work as expected on all platforms, sizes of int, etc? Or is there a more accepted way of doing it? (I made the following up.)

#define M         


        
5条回答
  •  佛祖请我去吃肉
    2021-01-28 12:37

    If you don't want to use defines (and you want a standard way of calculating the limits), then do this:

    #include 
    std::numeric_limits::min()
    

    These are the ANSI standard defines in limits.h:

    #define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */
    #define INT_MAX       2147483647    /* maximum (signed) int value */
    #define UINT_MAX      0xffffffff    /* maximum unsigned int value */
    

    These are the defines from BaseTsd.h:

    #define MAXUINT     ((UINT)~((UINT)0))
    #define MAXINT      ((INT)(MAXUINT >> 1))
    #define MININT      ((INT)~MAXINT)
    

提交回复
热议问题