I am a bit confused about the following if_even function. How is the return type FLAGS? This seems strange – isn’t an enum just a list of multiple integers to be used like #define‘s ?
enum flag_o_e {EVEN, ODD};
enum flag_o_e test1;
typedef enum flag_o_e FLAGS;
FLAGS if_even(int n);
main()
{
int x;
FLAGS test2;
printf("input an integer: "); scanf("%d", &x);
test2 = if_even(x);
if (test2 == EVEN)
printf("test succeeded (%d is even)\n", x);
else
printf("test failed (%d is odd)\n", x);system("pause");
}
FLAGS if_even(int n)
{
if (n%2)
return ODD;
else
return EVEN;
}
I appreciate any tips or advice.
Yes the enum declared at declared at the very top
enum flag_o_e {EVEN, ODD};is just the same as doingThen there is a further typedef to use FLAGS in place of flag_o_e so its essentially:
So when if_even() executes
return EVEN;its actually returning an int, and similarly the comparison of the resultif (test2 == EVEN)is comparing the result (which is really an int) with EVEN, which is also an int.typedef’s can get a little confusing, but its just a technique to rename a type (such as int, or ‘struct foo’) to something more readable and contextual.