TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - C Coding Standard
Search

SR:  C#1Checked automatically with code checker

Synopsis:Make sure enums are not mixed with other types
Language:C
Severity Level:3
Category:ISSUES IN K&R V. ANSI C


Description:

A significant grey area in the treatment of enums relates to their type. All enums are of integral type so it is possible to compare unrelated enums or even mix enums and integers without getting compiler warnings whereas this might result in unexpected behavior.

Examples:

typedef enum enum1 { ACT1, ACT2, ACT3 } enum1;
typedef enum enum2 { ACTION1, ACTION2, ACTION3 } enum2;

typedef struct {
   enum1 actionId1;
   enum2 actionId2;
   int type;
} actions;

void m(actions* p_sysreq) {
  if (p_sysreq->actionId1 == ACT1); // OK
  if (p_sysreq->actionId2 == ACT1); // Violation, comparison between 2 different enums
  if (p_sysreq->type == ACT1); // Violation, comparison between enum and int     
}