What will the following code print out?
#include <iostream>
enum Num {zero, one, two = 37, three, four};
int main(void) {
  Num number = three;
  int intNum = static_cast<int>(number);
  std::cout << intNum;
  return 0;
}
Explanation
Enum-type elements take on the following values by default: enum Num {zero = 0, one = 1, two = 2, three = 3, four = 4}. However, if one of the elements is explicitly assigned an integer value, then the values of all the elements following after it will change as well: enum Num {zero = 0, one = 1, two = 37, three = 38, four = 39} (see section 7.2 on page 112 in "International standard ISO / IEC 14882").

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes