enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void main()
{
int index = 0;
int count_of_letter = 0;
int count_of_space = 0;
char str[] = "I'm Ely efod";
match_flag = FALSE;
for(; str[index] != '/0'; index++)
if( SPACE != str[index] )
count_of_letter++;
else
{
match_flag = (enum BOOLEAN) 1;
count_of_space++;
}
printf("%s %d times %c", match_flag "match" : "not match", count_of_space, NEWLINE);
printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);
}
输出:
match 2 times
count of letters: 10
Press any key to continue
4. 枚举类型与sizeof运算符
#include <stdio.h>
enum escapes
{
BELL = '/a',
BACKSPACE = '/b',
HTAB = '/t',
RETURN = '/r',
NEWLINE = '/n',
VTAB = '/v',
SPACE = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void main()
{
printf("%d bytes /n", sizeof(enum escapes)); //4 bytes
printf("%d bytes /n", sizeof(escapes)); //4 bytes
printf("%d bytes /n", sizeof(enum BOOLEAN)); //4 bytes
printf("%d bytes /n", sizeof(BOOLEAN)); //4 bytes
printf("%d bytes /n", sizeof(match_flag)); //4 bytes
printf("%d bytes /n", sizeof(SPACE)); //4 bytes
printf("%d bytes /n", sizeof(NEWLINE)); //4 bytes
printf("%d bytes /n", sizeof(FALSE)); //4 bytes
printf("%d bytes /n", sizeof(0)); //4 bytes
}
5. 综合举例
#include<stdio.h>
enum Season
{
spring, summer=100, fall=96, winter
};
typedef enum
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;
void main()
{
/* Season */
printf("%d /n", spring); // 0
printf("%d, %c /n", summer, summer); // 100, d
printf("%d /n", fall+winter); // 193
Season mySeason=winter;
if(winter==mySeason)
printf("mySeason is winter /n"); // mySeason is winter
int x=100;
if(x==summer)
printf("x is equal to summer/n"); // x is equal to summer
printf("%d bytes/n", sizeof(spring)); // 4 bytes
/* Weekday */
printf("sizeof Weekday is: %d /n", sizeof(Weekday)); //sizeof Weekday is: 4
Weekday today = Saturday;
Weekday tomorrow;
if(today == Monday)
tomorrow = Tuesday;
else
tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday
}