cdonts
cdonts

Reputation: 9599

Undeclared enum?

While compiling this code:

#include <stdio.h>

enum Boolean
{
    TRUE,
    FALSE
};

int main(int argc, char **argv)
{
    printf("%d", Boolean.TRUE);

    return 0;
}

I'm getting:

error: 'Boolean' undeclared (first use in this function)

What I'm doing wrong?

Upvotes: 3

Views: 2904

Answers (4)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>

enum Boolean { FALSE, TRUE };

struct {
    const enum Boolean TRUE;
    const enum Boolean FALSE;
} Boolean = { TRUE, FALSE };

int main(){
    printf("%d\n", Boolean.TRUE);
    return 0;
}

Upvotes: 1

vanste25
vanste25

Reputation: 1774

Just write TRUE without Boolean.

Upvotes: 1

datenwolf
datenwolf

Reputation: 162164

You're wrote Boolean. Just write TRUE or FALSE without that prefix.

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 372784

In C, you don't access individually enumerated constants using the syntax EnumType.SpecificEnum. You just say SpecificEnum. For example:

printf("%d", TRUE);

When you write

printf("%d", Boolean.TRUE);

C thinks that you're trying to go to the struct or union named Boolean and access the TRUE field, hence the compiler error.

Hope this helps!

Upvotes: 6

Related Questions