网站建设赚钱,龙华、宝安最新通告,网站默认极速模式,产品设计和工业设计有什么区别C语言家教记录#xff08;七#xff09; 导语字符串字面量变量读写字符串操作函数惯用法数组 结构联合枚举总结与复习 导语
本次授课的内容如下#xff1a;字符串#xff0c;结构体、联合体、枚举
辅助教材为 《C语言程序设计现代方法#xff08;第2版#xff09;》
字… C语言家教记录七 导语字符串字面量变量读写字符串操作函数惯用法数组 结构联合枚举总结与复习 导语
本次授课的内容如下字符串结构体、联合体、枚举
辅助教材为 《C语言程序设计现代方法第2版》
字符串
字面量
双引号括起来的字符序列
to be or not to be, is a question字面量需要用\来延续\
printf(When you come to a fork in the road, take it. \
--Yogi Berra);//必须顶格字面量长度为n则存储空间为n1字符串也可以为空用单独的\0存储
char*sabc;//不能修改内容
char ch;
ch abc[1];
printf(\n);//非法只能是字面量变量
和整数一样也可以用数组
说明一下数据的实际存储探讨各情况下存储用的空间
char date1[8] June 14;
//等价于
char date1[8] {J, u, n, e, , 1, 4, \0};
char date2[9] June 14;
char date3[7] June 14;
char date4[] June 14;char *p;
char s[121];
pstr;读写字符串
用printf、scanf控制输入输出
char str[] Are we having fun yet?;
printf(%s\n, str);
printf(%.6s\n, str);//思考一下会输出什么
scanf(%s,str);用gets、puts控制输出
char s[121];
gets(s);//不知道数据长度有风险,fgets更好
puts(s);逐个读入
int read_line(char str[], int n)
{int ch, i 0;while ((ch getchar()) ! \n)if (i n)str[i] ch;str[i] \0; /* terminates string */return i; /* number of characters stored */
}示例程序
int count_spaces(const char s[])
{int count 0, i;for (i 0; s[i] ! \0; i)if (s[i] )count;return count;
}操作函数
strcpy,strlen,strcat,strcmp
strcpy(str2, abcd);
strcpy(str1, str2);
strcpy(str1, strcpy(str2, abcd));int len;
len strlen(abc); /* len is now 3 */
len strlen(); /* len is now 0 */
strcpy(strl, abc);
len strlen(strl);strcpy(str1, abc);
strcat(str1, def); /* str1 now contains abcdef */
strcpy(str1, abc);
strcpy(str2, def);
strcat(str1, str2);int strcmp(const char *s1, const char *s2);
if (strcmp(str1, str2) 0)惯用法
示例程序
size_t strlen(const char *s)
{const char *p s;while (*s)s;return s - p;
}{char *p s1;while (*p)p;while (*p *s2);return s1;
}数组
探讨存储方式区别
char planets[][8] {Mercury, Venus, Earth,Mars, Jupiter, Saturn,Uranus, Neptune, Pluto};char *planets[] {Mercury, Venus, Earth,Mars, Jupiter, Saturn,Uranus, Neptune, Pluto};示例程序
#include string.h
#define NUM_PLANETS 9
int main(int argc, char *argv[])
{char *planets[] {Mercury, Venus, Earth,Mars, Jupiter, Saturn,Uranus, Neptune, Pluto};int i, j;for (i 1; i argc; i) {for (j 0; j NUM_PLANETS; j)if (strcmp(argv[i], planets[j]) 0) {printf(%s is planet %d\n, argv[i], j 1);break;}if (j NUM_PLANETS)printf(%s is not a planet\n, argv[i]);}return 0;
}结构
struct {int number;char name[NAME_LEN1];int on_hand;
} part1, part2;
//介绍存储实现视为一整个变量可以认为生成了一个新的类型
struct{
char stu_name[10];
int id;
int grade;
}student;struct {int number;char name[NAME_LEN1];int on_hand;
} part1 {528, Disk drive, 10},part2 {914, Printer cable, 5};//初始化但是不推荐这么用;//通过.运算符进行访问或者用-
printf(Part number: %d\n, part1.number);
printf(Part name: %s\n, part1.name);
printf(Quantity on hand: %d\n, part1.on_hand);Part1.number 258; /* changes part1s part number */
Part1.on_hand;scanf(%d, part1.on_hand);part2 part1;struct { int a[10]; } a1, a2;
a1 a2;命名
struct part {int number;char name[NAME_LEN1];int on_hand;
};//一个新的类型struct part part1, part2;//不能直接用partstruct part {int number;char name[NAME_LEN1];int on_hand;
} part1, part2;typedef struct {int number;char name[NAME_LEN1];int on_hand;
} Part;//这之后可以用Part直接命名
示例程序
struct part build_part(int number, const char * name, int on_hand)
{struct part p;p.number number;strcpy (p.name, name);p.on_hand on_hand;return p;
}
part1 build_part(528, Disk drive, 10);其余部分见书
联合
解释一下存储实现
union {int i;double d;
} u;
union {int i;double d;
} u {0};示例程序
#define INT_KIND 0
#define DOUBLE_KIND 1
typedef struct {int kind; /* tag field */union{int i;double d;} u;
} Number;n.kind INT_KIND;
n.u.i 82;void print_number(Number n)
{if (n.kind INT_KIND)printf(%d, n.u.i);elseprintf(%g, n.u.d);
}枚举
#define SUIT int
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
enum {CLUBS, DIAMONDS, HEARTS, SPADES} s1, s2;
//等价于
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
enum suit s1, s2;
//等价于
typedef enum {CLUBS, DIAMONDS, HEARTS, SPADES} Suit;
Suit s1, s2;//c89中的bool
typedef enum {FALSE, TRUE} Bool;enum suit {CLUBS 1, DIAMONDS 2, HEARTS 3, SPADES 4};typedef struct {enum {INT_KIND, DOUBLE_KIND} kind;union {int i;double d;} u;
} Number;总结与复习
本次授课讲述第13章和第16章内容关键点字符串和新类型 文章转载自: http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn