What's the difference between these two enum declarations - C? -
i've started learning c , have reached point of enums. enum preferred alternative define
/ const int
, correct?
what's difference between these 2 declarations?
#include <stdio.h> // method 1 enum days { monday, tuesday }; int main() { // method 1 enum days today; enum days tomorrow; today = monday; tomorrow = tuesday; if (today < tomorrow) printf("yes\n"); // method 2 enum {monday, tuesday} days; days = tuesday; printf("%d\n", days); return 0; }
an enumeration should preferred on #define
/const int
when want declare variables can take on values out of limited range of related, mutually exclusive values. days of week good example, bad example:
enum aboutme { myage = 27, mynumberoflegs = 2, myhousenumber = 54 };
going code example; first method declares type called enum days
. can use type declare many variables like.
the second method declares single variable of type enum { ... }
. cannot declare other variables of type.
Comments
Post a Comment