|
|||||||||||
|
Technical Support Support Resources
Product Information |
Technical SupportC51: TYPEDEF ENUMInformation in this article applies to:
QUESTIONI 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? ANSWERIt 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 08, 2011 | ||||||||||
|
|||||||||||