Returning an enum from a function in C?

In C++, you could use just Foo.

In C, you must use enum Foo until you provide a typedef for it.

And then, when you refer to BAR, you do not use Foo.BAR but just BAR. All enumeration constants share the same namespace (the “ordinary identifiers” namespace, used by functions, variables, etc).

Hence (for C):

enum Foo { BAR, BAZ };

enum Foo testFunc(void)
{
    return BAR;
}

Or, with a typedef:

typedef enum Foo { BAR, BAZ } Foo;

Foo testFunc(void)
{
    return BAR;
}

I believe that the individual values in the enum are identifiers in their own right, just use:

enum Foo testFunc(){
  return BAR;
}

Tags:

C

Function

Enums