vlid ways to declare an enum
Code:
enum boolean { false, true };
enum boolean check;
enum boolean
{
false, true
}
include <stdio.h>
enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
int main()
{
enum week today;
today = wednesday;
printf("Day %d",today+1);
return 0;
}
enum MyNumsy { FIVE, FOUR, THREE, TWO, ONE };
enum MyNumsy mynumy;
#include <stdio.h>
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
int main()
{
card = club;
printf("Size of enum variable = %d bytes", sizeof(card));
return 0;
}
enum designFlags {
ITALICS = 1,
BOLD = 2,
UNDERLINE = 4
} button;
C Enumeration (Enum): Examples and Where it is used?
I use casting to the enum in my debugging by printf which a lot of people use printf rather than a debugger, their
are more than one way of doing things. If one knows their enum and which number it is then they can check it by
casing to insure they are getting the right one. which depends on the circumstances of what they are doing. their
is a valid use for it. showing the use of it by number of the element which the assignment of a different number
as already been shown, but by using the given number 0 ... whatever the last one is. it shows how it works by giving both
examples and shows that it does not have to be in order 0 ... 19 etc.. it can be mixed up, because the
switch works off the value within the variable name, not in sequence. 1,2,3,4,5,6,7,8,9....
the sequence one writes it in a switch does not matter as shown in the example. so while not knowing what element
number it is does not have to be know, it does not hurt to know it. it is left up to the programmer to
know or not to know. Not someone dictating that they have to or not have to know it.
flexibility is what is had. to assigning a number to an enum and you have to know what that number
means as given in you example of Http errors. so now flexibility is had? You can either print out the http error
number, or you have to know what that error number represents to know what error name it is, so you
can print out that error name, or number.
so using an enum then assigning the http error 'code' number to each one, then what?
Code:
enum Http_Status { OK = 200, FORBIDDEN = 403, PAGE_NOT_FOUND = 404, INTERNAL_SERVER_ERROR = 500 }
if ( FORBIDDEN )
print(403);
or one now longer needs to even know what that code number is anymore because they are now using an enum
instead? I'd think they'd have to better know what is what and what goes to what else they get confused and
maybe even assigning the wrong code to the wrong name? The way I see it . it all has its place, it is up to the
programmer how he or she uses it to benefit from it. and I never said one had to print out the element
number to the which ever element it is. it can be used for self knowledge while working with the code.
either way your argument over this with me is made invalid by your use of example of http error codes