rakd
rakd

Reputation: 185

How to refer to a enum member when there is a local variable exists with the same name as the enum member?

#include<stdio.h>

enum color {b,g,i,k};

void main()
{
   int b=100; //line 7
   enum color out=b; //line 8
   printf("%d\n",out);
}

In line 8 the b which i am referring to is color.b not the one which declared at line 7 .In C++ i can refer b from color as color::b, in C How do i do this?

Upvotes: 1

Views: 101

Answers (2)

Antonin GAVREL
Antonin GAVREL

Reputation: 11259

You can also do it the following way:

#include<stdio.h>

struct color_s {
    int b,g,i,k;
} const color = {0,1,2,3};

void main()
{
   int b=100; //line 7
   int out = color.b; //line 8
   printf("%d\n",out);
}

Upvotes: 1

icebp
icebp

Reputation: 1709

As a comment already mentioned, enumerations are part of the global scope. Usually, a prefix is used to distinguish them, for example:

enum color {
    cB, cG, cI, cK
}

Upvotes: 1

Related Questions