实例将枚举类型的赋值与基本数据类型的赋值进行了对比:
方法1:先声明变量,再对变量赋值
#include<stdio.h>/* 定义枚举类型 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }
void main()
{
/* 使用基本数据类型声明变量,然后对变量赋值 */
int x, y, z
x = 10
y = 20
z = 30
/* 使用枚举类型声明变量,再对枚举型变量赋值 */
enum DAY yesterday, today, tomorrow
yesterday = MON
today = TUE
tomorrow = WED
printf("%d %d %d \n", yesterday, today, tomorrow)
}
方法2:声明变量的同时赋初值
#include <stdio.h>/* 定义枚举类型 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }
void main()
{
/* 使用基本数据类型声明变量同时对变量赋初值 */
int x=10, y=20, z=30
/* 使用枚举类型声明变量同时对枚举型变量赋初值 */
enum DAY yesterday = MON,
today = TUE,
tomorrow = WED
printf("%d %d %d \n", yesterday, today, tomorrow)
}
方法3:定义类型的同时声明变量,然后对变量赋值。
#include <stdio.h>/* 定义枚举类型,同时声明该类型的三个变量,它们都为全局变量 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow
/* 定义三个具有基本数据类型的变量,它们都为全局变量 */
int x, y, z
void main()
{
/* 对基本数据类型的变量赋值 */
x = 10 y = 20 z = 30
/* 对枚举型的变量赋值 */
yesterday = MON
today = TUE
tomorrow = WED
printf("%d %d %d \n", x, y, z) //输出:10 20 30
printf("%d %d %d \n", yesterday, today, tomorrow) //输出:1 2 3
}
方法4:类型定义,变量声明,赋初值同时进行。
#include <stdio.h>/* 定义枚举类型,同时声明该类型的三个变量,并赋初值。它们都为全局变量 */
enum DAY
{
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
yesterday = MON, today = TUE, tomorrow = WED
/* 定义三个具有基本数据类型的变量,并赋初值。它们都为全局变量 */
int x = 10, y = 20, z = 30
void main()
{
printf("%d %d %d \n", x, y, z) //输出:10 20 30
printf("%d %d %d \n", yesterday, today, tomorrow) //输出:1 2 3
}
2、对枚举型的变量赋整数值时,需要进行类型转换。
#include <stdio.h>enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }
void main()
{
enum DAY yesterday, today, tomorrow
yesterday = TUE
today = (enum DAY) (yesterday + 1) //类型转换
tomorrow = (enum DAY) 30 //类型转换
//tomorrow = 3 //错误
printf("%d %d %d \n", yesterday, today, tomorrow) //输出:2 3 30
}
3、使用枚举型变量
#include<stdio.h>enum
{
BELL = '\a',
BACKSPACE = '\b',
HTAB = '\t',
RETURN = '\r',
NEWLINE = '\n',
VTAB = '\v',
SPACE = ' '
}
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)
}
#include <stdio.h>struct zh
{
char name[20]
int haoma
enum {w,m}xb
char zhiye
union nn
{
int clas
char zhiwu[20]
}th
}
int main()
{
int n,i,j
struct zh kk[100]
char ch
scanf("%d",&n)
ch=getchar()
for(i=0i<ni++)
{
scanf("%s",kk[i].name)
scanf("%d",&kk[i].haoma)
scanf("%d",&j)
if(j==0)
kk[i].xb=w
else
kk[0].xb=m
printf("=======%d\n",kk[0].xb)
}
return 0
}
就加了一条输出语句
你给的枚举变量已经赋值了,是默认的
以下是百度上的内容,我觉得写得很正确;
枚举是用标识符表示的整型常数集合,这些常数是该类型变量可取的合法值,这些标识符称为枚举常量。
定义:enum<枚举名>{<标识符1>,<标识符2>...<标识符n>}
比如enum weekday{sun,mon,tue}
就表示定义了3个枚举型常量,默认他们的值是从0开始,依次递增,也就是sum=0,mon=1,tue=2
你也可以直接给他们赋值,比如enum weekday{sun=1,mon,tue}
如果这样的话mon的值就为2,tue的值就为3.
你也可以用其他变量代替枚举常量的值,比如
enum weekday{sun,mon,tue}a,b,c
a=sun
b=mon
c=tue
如果输出的话a=0,b=1,c=2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)