ARM Technical Support Knowledge Articles

TYPEDEF ENUM

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I have recently tried this code snippet:

#include 

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.

Normally, 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.

Article last edited on: 2011-03-08 13:42:42

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential