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 ?
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.
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With