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 ?

2
  • 1
    What language ? Please tag appropriately. Note that this would give you a compile-time error in most sensible languages. Commented Jun 4, 2014 at 10:41
  • Sorry , forgot to write it , I want to know this in CAPL as well as C Commented Jun 4, 2014 at 10:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Sorry , Sounds I did not get you correctly , does that mean a compilation error , or I should handle all of the cases in the function implementation ?
@MohammedAbdelhamid: Please see the update to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.