Keil Logo Arm Logo

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.

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.

Last Reviewed: Tuesday, March 8, 2011


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

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

ARM’s Privacy Policy has been updated. By continuing to use our site, you consent to ARM’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.