
- 枚举
- 结构
- 联合
程序中出现数字要尽量用符号来表示而非直接把数字出现在程序中(好处:可读性)
这时会使用到int、const等定义变量的关键字
枚举的出现:定义一些排比的符号量 比int、const更加方便
定义格式
enum 枚举名 {元素1,元素2…};
C语言内部enum数据类型一般为int
基本枚举使用
#include
enum COLOR {RED, YELLOW, GREEN}; //声明一种为color的数据类型
int main(int argc, char const *argv[])
{
int color = -1;
char *colorName = NULL;
printf("输入喜欢颜色代码");
scanf("%d", &color);
switch (color) {
case RED:
colorName = "red";
break;
case YELLOW:
colorName = "yellow";
break;
case GREEN:
colorName = "green";
break;
default:
colorName = "unknown";
break;
}
printf("喜欢的颜色:%s", colorName);
return 0;
}
枚举类型可以作为值
枚举类型可以跟上enum作为类型
#include
enum color { red, yellow, green};
void f(enum color c);
int main(void)
{
enum color t = red;
scanf("%d", &t);
f(t);
return 0;
}
void f(enum color c)
{
printf("%d\n",c);
}
自动计数的枚举
好处:用枚举类型中最后一个量来定义数组或或实现循环
#include
enum COLOR {RED, YELLOW, GREEN, NumCOLORS};
int main(int argc, char const *argv[])
{
int color = -1;
char *ColorNames[NumCOLORS] = {
"red","yellow","green",
}; //定义一个字符串数组,大小以枚举最后一个量为定义
char *colorName = NULL;
printf("喜欢颜色代码");
scanf("%d", &color);
if (color >=0 && color <NumCOLORS){
colorName = ColorNames[color];
}else{
colorName="unknown";
}//判断输入条件来输出结果
printf("like clor:%s", colorName);
return 0;
}
声明枚举量时可以指定值
#include
enum COLOR {RED=1, YELLOW, GREEN=5, NumCOLORS}; //直接规定枚举量的指定值
int main(int argc, char const *argv[])
{
enum COLOR color = 0;
printf("code for GREEN is %d\n", GREEN);
return 0;
}
结构
表达数据需要有变量,每个变量都需要有类型
一个结构就是一个复合的东西,用一个变量来表达所有的数据
通常在函数外部声明结构类型,这样可以被多个函数所使用
常用的结构定义方式
struct point{
int x;
int y;
}p1,p2;
基本使用
#include
struct date {
int month;
int day;
int year;
}; //声明一种新的类型
int main(int argc, char const *argv[])
{
struct date today; //定义这种结构类型的变量
today.month = 07;
today.day = 31;
today.year = 2014;
printf("Today's date is %i-%i-%i.\n",today.year, today.month, today.day);
return 0;
}
结构初始化
#include
struct date{
int month;
int day;
int year;
}; //定义一个结构变量
int main(int argc, char const *argv[])
{
struct date today = {07,31,2014}; /对结构变量进行初始化
struct date thismonth = {.month=7, .year=2014};
printf("Today's date is %i-%i-%i.\n",today.year,today.month,today.day);
printf("This month is %i-%i-%i.\n",thismonth.year,thismonth.month,thismonth.day);
}
结构与函数
整个结构是可以作为参数传入函数的
#include
#include
struct date {
int month;
int day;
int year;
};
bool isLeap(struct date d); //判断所在那一年是否为闰年
int numberOfDays(struct date d);
int main(int argc, char const *argv[])
{
struct date today, tomorrow;
printf("Enter today's date (mm dd yyyy):");
scanf("%i %i %i", &today.month, &today.day, &today.year);
if (today.day != numberOfDays(today)) //直接将结构变量传给函数
{
tomorrow.day = today.day+1;
tomorrow.month = today.month;
tomorrow.year = today.year;
}else if ( today.month ==12 ){
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
}else{
tomorrow.day = 1;
tomorrow.month = today.month+1;
tomorrow.year = today.year;
}
printf("Tomorrow's date is %i-%i-%i.\n",tomorrow.year,tomorrow.month, tomorrow.day);
return 0;
}
int numberOfDays(struct date d)
{
int days;
const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; //用数组12个月都有多少天
if (d.month ==2 && isLeap(d)) //若为闰年且为二月
days = 29;
else
days = daysPerMonth[d.month-1];
return days; //单一出口return
}
bool isLeap(struct date d) //判断是否为闰年
{
bool leap = false;
if ((d.year %4 ==0 && d.year %100 !=0) || d.year%400 ==0)
leap = true;
return leap;
}
结构中嵌套结构
#include
struct point{
int x;
int y;
};
struct rectangle{
struct point p1;
struct point p2;
};
void printRect(struct rectangle r)
{
printf("<%d, %d> to <%d, %d>\n",r.p1.x, r.p1.y, r.p2.x, r.p2.y);
}
int main(int argc, char const *argv[])
{
int i;
struct rectangle rects[] = {
{{1,2},{3,4}},
{{5,6},{7,8}}
}; //最外层大括号表示的数组,第二层表示数组的单元,
for (i=0;i<2;i++)
{
printRect(rects[i]);
}
}
联合
typedef
每次用结构类型时都要跟上关键字,摆脱关键字,重新起名字用到typedef
#include
typedef struct ADate {
int month;
int day;
int year;
}Date;
int main()
{
Date d = {1,2,2005}; //定义结构中的一个变量
printf("%d年%d月%d日", d.yar, d.month, d.day);
}
union
与struct非常相似
区别:
struct中有两个值,它的两个值是截然分开的
对于union来说,这两个成员占据了相同的内存空间,联合起来使用同一个空间
#include
typedef union {
int i;
char ch[sizeof(int)]; //定义一个以int字节大小为大小的数组
}CHI;
int main(int argc, char const *argv[])
{
CHI chi; //定义联合中的变量
int i;
chi.i = 1234;
for ( i=0; i<sizeof(int); i++){
printf("%02hhx", chi.ch[i]);
}
printf("\n");
return 0;
}
用到union的几种可能:
1、通过union得到一个整数内部的各个字节
2、可以得到一个double、float内部字节
3、文件 *** 作时将一个整数以二进制形式写入文件中时 用来做读写的一个中间的媒介
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)