3

I've got a question

What shall happen if we have the following :

typedef enum  {s1=0,s2,s3} states ;

void test( states x ) ;

when using the function test , what happens if I use it like the following :

test(6);

Shall it be mapped to the nearest enum value , or it needs to be handled in the function implementation ?

Paul R
  • 208,748
  • 37
  • 389
  • 560

2 Answers2

2

enum are effectively treated as int by most C compilers. They are just syntactic sugar to make code more readable. In your case, 6 will be passed to the function and the function has to handle it.

Akhi
  • 190
  • 1
  • 1
  • 6
1

[for C]

If doing

test(6);

6 will be passed to test() (as enums are (treated as) ints) and it shall be trapped by the function's input validation.


Update:

Input validation is not done automagically. It needs to be coded explicitly.

alk
  • 69,737
  • 10
  • 105
  • 255