There is this code.
#include <iostream>
#include <climits>
enum e {zero,one};
void main()
{
e num=(e)INT_MAX;
std::cout<<num;
}
Is it defined by the standart that output will be 2147483647?(on condition sizeof(int)=4 bytes)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No; when converting an integer to an enumeration, the enumeration value is only specified if the integer value is within range. From the standard:
where the “range” is described in a rather complicated manner in 7.2/7, and essentially goes up to the smallest value of
2^M-1that’s no less than the largest defined value.The compiler is allowed to use any integer type to represent the enumeration, as long as it’s large enough to hold all the enumeration values; so in this case, it’s free to use a smaller type than
int, such aschar. Also,INT_MAXis only guaranteed to be at least 32767.