Keil™, An ARM® Company

Technical Support

C51: TYPEDEF ENUM


Information in this article applies to:

  • C51 All Versions

QUESTION

I have recently tried this code snippet:

#include <stdio.h>

typedef enum
{
   ENQ_IDLE = 0,
   ENQ_ACTIVE
} UDE_enq_cmd_state_t;


/*
enum UDE_enq_cmd_state_t {

   ENQ_IDLE = 0,
   ENQ_ACTIVE
   };
typedef enum UDE_enq_cmd_state_t EnqCmdState;
*/

UDE_enq_cmd_state_t EnqCmdState;

void main(void)
{
unsigned char testy;


EnqCmdState = ENQ_IDLE;
EnqCmdState = 15;

   while(1)
   {
      testy++;
   }


}

and it compiles and links with no errors or warnings.

Nornally, I would expect an error on the line: EnqCmdState = 15; otherwise using the typedef is a bit of a waste of time. How do I use this to write more defensive programming?

ANSWER

It is legal in C to assign an 'enum' variable a constant value. Several embedded compilers support this, as does Microsoft Visual C. Code checking tools such as PC-LINT will flag code that attempts to assign constants to an enum.

Last Reviewed: Wednesday, May 10, 2006


Did this article provide the answer you needed?
 
Yes
No
Not Sure