I am a bit confused by what for and where enum is good? What are the benefits of using enum against defining global variables (constants) or macros? For instance in this code:
#include<stdio.h> enum bool {false, true}; int main() { enum bool b = 100; printf("%d", b); return 0; } I can assign even any integer value to b and every thing works fine, so why not doing
int false = 0, true = 1; or
#define false 0 #define true 1 in the global scope? Can some one explain where and why are enums useful and should be preferred to use?
enumtype, the compiler understands all possible values for the value.int, there's no way to tell that it should only contain ` true/false` value.enum cartype { sedan, suv, hatchback, coupe };). One advantage of using enumeration constants over preprocessor macros is that the constant name is preserved in debuggers, where the macro is not (e.g., if you have an objectenum cartype car = sedan;and you examine the value in the debugger, the debugger will showsedaninstead of0).stdboolinstead of defining your owntrue/falsevalues.