numeric-limits

Why is 0 < -0x80000000?

风流意气都作罢 提交于 2019-11-26 06:11:36
问题 I have below a simple program: #include <stdio.h> #define INT32_MIN (-0x80000000) int main(void) { long long bal = 0; if(bal < INT32_MIN ) { printf(\"Failed!!!\"); } else { printf(\"Success!!!\"); } return 0; } The condition if(bal < INT32_MIN ) is always true. How is it possible? It works fine if I change the macro to: #define INT32_MIN (-2147483648L) Can anyone point out the issue? 回答1: This is quite subtle. Every integer literal in your program has a type. Which type it has is regulated by

maximum value of int

北战南征 提交于 2019-11-26 02:41:37
问题 Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java? 回答1: In C++: #include <limits> then use int imin = std::numeric_limits<int>::min(); // minimum value int imax = std::numeric_limits<int>::max(); std::numeric_limits is a template type which can be instantiated with other types: float fmin = std::numeric_limits<float>::min(); // minimum positive value float fmax = std::numeric_limits<float>::max(); In C: