Lovelyp4u
Lovelyp4u

Reputation: 21

What is actual purpose of enum as function argument?

Until now I thought if a function argument is an enum, then it can hold only the values defined inside the enum. But it proved wrong in my case.

Code:

typedef enum
{
    a = 0,
    b,
    c
} X;

typedef enum
{
    e = 3
} Y;

void fn (X var)
{
    printf ("%d",var);
}

int main()
{
    fn (e);
    return 0;
}

Function fn() accepted the value e and its output was 3. Why does it happen?

Upvotes: 2

Views: 214

Answers (1)

user2431714
user2431714

Reputation: 31

Any integer value can be used where an enum is required. The compiler does not validate the integer values.

They are basically a convenient way to assign meaningful names to what would otherwise appear as arbitrary numbers in your code.

Upvotes: 3

Related Questions