0

Can someone please explain to me which part is what in this:

enum bb { cc } dd;

I understand that enum bb is the name of the enumeration and that { cc } is its enumerator, but I have no clue what dd is.

Veanty
  • 33
  • 5

2 Answers2

3
enum bb
{
  cc
} dd;
  1. bb - enum tag.
  2. cc - enumerator constant. As it is first and it does not have an expicity defined value it will be zero (0);
  3. dd - variable of type enum bb

It can be written as:

enum bb
{
  cc
};

enum bb dd;
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks! That helped me, but I still wonder, why does printf("%d", dd) print 0? – Veanty Jan 30 '23 at 13:29
  • 1
    If it was defined in the file scope then it will be initialized to zero, if in the function scope and not implicitly initialized then the value is not determined (it is Undefined Behaviour) so it can behave any way – 0___________ Jan 30 '23 at 13:31
  • @Veanty As 0___________ says, it is undefined behaviour to use the value of an uninitialized local variable. Undefined behaviour is the most severe grade of non-specified behaviour in the C standard and even to experienced programmers, it may be surprising that using an uninitialized variable falls in that category. There is a nice explanation here: https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior. – nielsen Jan 30 '23 at 14:02
1

It defines dd as a variable of the type enum bb. Take a look at Enumerations It behaves just like when you're defining normally

#include <stdio.h>
int main()
{
    enum bb { cc, ee } dd  = cc; // dd is now 0
    dd = ee; // dd is now 1
    printf("%d", dd); 
}

Link.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115