Uninitialized enum Variables

Uninitialized enum Variables

The value of an uninitialized automatic enum variable is indeterminate. Uninitialized global and static enum variables have a zero value by default. In this regard, enum variables are similar to fundamental types such as int and char. However, unlike int and char, an uninitialized enum might have a value that is beyond the range of its enumerators. In the following example, the global enum variable st is initialized to zero. Yet none of Stat’s enumerators equals zero. Therefore, using st before assigning a valid value to it results unpredictable behavior:

 enum Stat {good=1, bad=2}; // no enumerator equals zeroStat st; // bad, initialized to 0 by defaultint main(){ if(st==good) //always false //


Share the Post: