书籍整理内容:
最近在看C primer plus(加深巩固自己的C语言技巧,为以后学习C++打个基础)。
里面知识针对自己以后要查的点整理出来。
使用工具:visual studio 2013
第二、三章内容:概述、变量、基本数据类型等:https://blog.csdn.net/answerMack/article/details/103766020
第四章内容:字符串和格式化输入输出:https://blog.csdn.net/answerMack/article/details/103805900
第五章内容:运算符、表达式和语句:https://blog.csdn.net/answerMack/article/details/103855794
第六章内容:循坏、赋值运算符等:https://blog.csdn.net/answerMack/article/details/103870182
第七章内容:if、if else、?:、switch、goto、continue、逻辑运算符优先级https://blog.csdn.net/answerMack/article/details/103891048
第八章内容:字符输入输出函数、输入验证(混合输入)https://blog.csdn.net/answerMack/article/details/103953376
第九章内容:函数和指针:https://blog.csdn.net/answerMack/article/details/103978471
第十章:数组和指针:https://blog.csdn.net/answerMack/article/details/104114028
puts()和printf()
相同:输出函数
不同:puts()只显示字符串且自动在字符串末尾加换行符。
字符串常量
双引号括起来的内容为字符串常量(字符串字面量),自动在末尾加入\0字符,属于静态存储类别,只会被存储一次。
字符数组
字符数组和其他数组名一样,是该数组首元素的地址。
指针表示法:
数组表示法
不同:
数组中的字符串被储存两次:字符串常量(静态存储区)、数组中的字符串(动态存储区(程序运行才会分配内存))
#include <stdio.h>
#define SLEN 40
#define LIM 5
int main(void)
{
const char *mytalents[LIM] = {
"Adding numbers swiftly",
"Multiplying accurately", "Stashing data",
"Following instructions to the letter",
"Understanding the C language"
};
char yourtalents[LIM][SLEN] = {
"Walking in a straight line",
"Sleeping", "Watching television",
"Mailing letters", "Reading email"
};
int i;
puts("Let's compare talents.");
printf("%-36s %-25s\n", "My Talents", "Your Talents");
for (i = 0; i < LIM; i++)
printf("%-36s %-25s\n", mytalents[i], yourtalents[i]);
printf("\nsizeof mytalents: %zd, sizeof yourtalents: %zd\n",
sizeof(mytalents), sizeof(yourtalents));
getchar();
return 0;
}
字符串输入
读取字符串的函数
char word[20];
int a;
scanf()
scanf()—用法见第四章
scanf()使用指向变量的指针
字符串到字符数组,不需要使用&;
读取其他变量,需要在变量名前加&。
scanf()函数用%s 只能读取一个单词
scanf("%d",&a);
scanf("%s",word);
多类型输入可选择scanf函数。
gets()输入函数
puts()输出函数
gets()函数可以读取整行输入,直到遇到换行符。后丢弃换行符,储存其他字符,并在末尾添加空字符构成字符串。
gets(word);
在vs中使用gets_s函数替代gets函数
puts()函数只显示字符串且自动在字符串末尾加换行符
puts(word);
puts(“i am ok.”)==printf(“i am ok.\n”);
当缓字符串过长,缓冲区溢出报错:
fgets
fgets()输入函数
fputs()输出函数
fgets()函数通过第2个参数限制读入的字符数来解决溢出的问题,是专门设计用于处理文件输入的函数。
fgets(word,20,stdin);//stdin:从键盘读入
fputs(word,stdout);//stdout:标准输出:显示在显示器上。
fgets()和gets()区别:
puts()和fputs()区别:
#include <stdio.h>
#define STLEN 10
int main(void)
{
char words[STLEN];
puts("Enter strings (empty line to quit):");
while (fgets(words, STLEN, stdin) != NULL && words[0] != '\n')
fputs(words, stdout);
puts("Done.");
return 0;
}
结果:
分析程序:
while条件:
(1)fgets(words, STLEN, stdin) != NULL:即字符串读取完成(会读取换行符),读取文件结尾才会有NULL。
(2)words[0] != ‘\n’:即首行不为换行符
程序结果:
因为数组长度为10,会循环读取
(1)将换行符替换为空字符:
(2)若目标数组装不下一整行输入,就丢弃多出的字符。
例子:
#include <stdio.h>
#define STLEN 10
int main(void)
{
char words[STLEN];
int i;
puts("Enter strings (empty line to quit):");
while (fgets(words, STLEN, stdin) != NULL
&& words[0] != '\n')
{
i = 0;
while (words[i] != '\n' && words[i] != '\0')
i++;
if (words[i] == '\n')
words[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
puts(words);
}
puts("done");
return 0;
}
结果:
解析:fgets()函数一次只能读取STLEN-1个字符构成字符串,其末尾有两种情况
(1)字符少于STLEN-1,且末尾有\n换行符
(2)字符多于STLEN-1,则会去读其中的STLEN-1字符,添加\0组成字符串。若不解决,则会循环读取,全部显示。
第一种情况解决:fgets()函数会读取换行符,而对空字符,fgets会返回NULL,因此用puts()函数需将换行符替换为NULL
第二种情况解决:因为字符多于STLEN-1,则须将多余STLEN的字符串丢弃,因此读取words字符串,遇到\0后,将其余的读取但不存储,丢弃。
gets_s()函数
gets_s(word);
gets_s(word,20);
gets_s()与fgets()区别:
gets、gets_s、fgets三者比较:
(1)目标存储区装得下:三个函数都可以,但fgets()会保留换行符
(2)输入行太长:
gets()不安全
gets_s()需要编写特殊函数(或程序中止或退出)。
fgets()可以循环读取,也可丢弃超出的字符。
创建s_gets()函数
利用fgets()函数返回指向char的指针。
字符串输出
puts()
fputs()
printf()
自定义输入输出函数
字符串函数
处理字符串的函数在string.h文件中,常用的有:
strlen() 、 strcat()、 strcmp()、 strncmp() 、 strcpy()、 strncpy()、sprintf()原型在stdio.h文件中、
strlen()
统计字符串长度
strlen(word);
strcat()
strcat()用于拼接字符串,接受两个字符串为参数,将第2个字符串备份附加在第1个字符串末尾,然后拼接后形成新字符串1,第2字符串不变。
函数类型为 char *
strcat函数返回的第一个参数为新字符串1的地址。
strcat(word,mesg);
strncat()
#include <stdio.h>
#include <string.h>
#define SIZE 30
#define BUGSIZE 13
char * s_gets(char * st, int n);
int main(void)
{
char flower[SIZE];
char addon[] = "s smell like old shoes.";
char bug[BUGSIZE];
int available;
puts("What is your favorite flower?");
s_gets(flower, SIZE);
if ((strlen(addon) + strlen(flower) + 1) <= SIZE)
strcat(flower, addon);
puts(flower);
puts("What is your favorite bug?");
s_gets(bug, BUGSIZE);
available = BUGSIZE - strlen(bug) - 1;
strncat(bug, addon, available);
puts(bug);
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
结果:
超限结果:
strcmp()
用户的响应与已储存的字符串作比较。
其中:
#include <stdio.h>
#include <string.h>
#define SIZE 80
#define LIM 10
#define STOP "quit"
char * s_gets(char * st, int n);
int main(void)
{
char input[LIM][SIZE];
int ct = 0;
printf("Enter up to %d lines (type quit to quit):\n", LIM);
while (ct < LIM && s_gets(input[ct], SIZE) != NULL &&
strcmp(input[ct], STOP) != 0)
{
ct++;
}
printf("%d strings entered\n", ct);
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
strncmp()
strncmp(字符串1,字符串2,比较指定的字符数)
顺序比较,前几个指定的字符数
#include <stdio.h>
#include <string.h>
#define LISTSIZE 6
int main()
{
const char * list[LISTSIZE] =
{
"astronomy", "astounding",
"astrophysics", "ostracize",
"asterism", "astrophobia"
};
int count = 0;
int i;
for (i = 0; i < LISTSIZE; i++)
if (strncmp(list[i], "astro", 3) == 0)
{
printf("Found: %s\n", list[i]);
count++;
}
printf("The list contained %d words beginning"
" with astro.\n", count);
return 0;
}
strcpy() strncpy()
strcpy() 拷贝整个字符串,相当于给字符串赋值。
#include <stdio.h>
#include <string.h> // declares strcpy()
#define WORDS "beast"
#define SIZE 40
int main(void)
{
const char * orig = WORDS;
char copy[SIZE] = "Be the best that you can be.";
char * ps;
int a;
puts(orig);
puts(copy);
a=strcpy_s(copy + 7,strlen(orig)+1, orig);
puts(copy);
//puts(ps);
printf("%d\n", a);
return 0;
}
strncpy():
strncpy_s(qwords[i], temp, 7 - 1);
/* copy3.c -- strncpy() demo */
#include <stdio.h>
#include <string.h> /* declares strncpy() */
#define SIZE 40
#define TARGSIZE 7
#define LIM 5
char * s_gets(char * st, int n);
int main(void)
{
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i = 0;
printf("Enter %d words beginning with q:\n", LIM);
while (i < LIM && s_gets(temp, SIZE))
{
if (temp[0] != 'q')
printf("%s doesn't begin with q!\n", temp);
else
{
strncpy_s(qwords[i], temp, TARGSIZE - 1);
qwords[i][TARGSIZE - 1] = '\0';
i++;
}
}
puts("Here are the words accepted:");
for (i = 0; i < LIM; i++)
puts(qwords[i]);
return 0;
}
strncpy(target,source,n);
把source中的n个字符或空字符之前的字符拷贝到target中。
若拷贝字符数小于source字符数,则未拷贝空字符。
sprintf()
sprintf()在stdio.h文件中。是把数据写入字符串,不是打印显示。
#include <stdio.h>
#define MAX 20
char * s_gets(char * st, int n);
int main(void)
{
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
double prize;
puts("Enter your first name:");
s_gets(first, MAX);
puts("Enter your last name:");
s_gets(last, MAX);
puts("Enter your prize money:");
scanf_s("%lf", &prize);
sprintf_s(formal, "%s, %-19s: $%6.2f\n", last, first, prize);
puts(formal);
return 0;
}
其他字符串函数
字符串示例:排序
#include <stdio.h>
#include <string.h>
#define LIMB 5
#define HIMB 40
char * s_gets(char * st, int n);
void paixu(char *string[], int num);
int main(void) {
char CMPS[LIMB][HIMB];
char * ptr[LIMB];
int i = 0;
puts("shu ru zhi:");
while (i<LIMB&&s_gets(CMPS[i],HIMB)!=NULL&&CMPS[i][0]!='\0')
{
ptr[i] = CMPS[i];
i++;
}
paixu(ptr, i);
puts("jie guo wei :");
for (int a = 0; a < i;a++)
{
puts(ptr[a]);
}
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
void paixu(char *string[], int num) {
char * temp;
int top, seek;
for (top = 0; top < num - 1; top++)
for (seek = top + 1; seek < num;seek++)
if (strcmp(string[top],string[seek])>0)seek>top
{
temp = string[top];
string[top] = string[seek];
string[seek] = temp;
}
}
排序的是指针,并不是字符串。
排序方法为:
高级排序函数:qsort();见第16章
ctype.h字符函数和字符串
第七章讲过:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LIMIT 8
void ToUpper(char *);
int PunctCount(const char *);
int main(void)
{
char line[LIMIT];
char * find;
puts("Please enter a line:");
fgets(line, LIMIT, stdin);
find = strchr(line, '\n'); // look for newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
ToUpper(line);
puts(line);
printf("That line has %d punctuation characters.\n",
PunctCount(line));
return 0;
}
void ToUpper(char * str)
{
while (*str)
{
*str = toupper(*str);
str++;
}
}
int PunctCount(const char * str)
{
int ct = 0;
while (*str)
{
if (ispunct(*str))
ct++;
str++;
}
return ct;
}
命令行参数:int main(int argc,char * argv [])
例子:main函数中含参数,argc(argument count 参数计数)argv(argument value 参数值)为指针数组。
#include <stdio.h>
int main(int argc, char *argv[])
{
int count;
printf("The command line has %d arguments:\n", argc - 1);
for (count = 1; count < argc; count++)
printf("%d: %s\n", count, argv[count]);
printf("\n");
getchar();
return 0;
}
运行方法:
找到文件目录:
打开cmd,命令行窗口
输入命令:
字符串转为数字
#include <stdio.h>
#include <stdlib.h>//程序包含此头文件,包含了atoi函数原型
//#include <string.h>
int main(int argc, char ** argv) {
int times;
if (argc<2||(times=atoi(argv[1]))<1)
{
printf("%s shi cuo wu de. \n", argv[0]);
}
else
{
for (int i = 0; i < times;i++)
{
printf("hhhhhhhhhh.\n");
}
}
return 0;
}
stdlib.h
附录B 页数p698
strtol()
函数原型:
long strtol(const char * restrict nptr, char ** restrict endptr,int base);
nptr是指向待转换字符串的指针,
endptr是一个指针的地址,该指针被设置为标识输入数字结束字符的地址,如‘\0’ '\n’或输入数字后面的其他字符等。
base表示以什么进制写入数字。
restrict 限制函数参数的用法,例如不能将字符串拷贝给本身。详见16章。
#include <stdio.h>
#include <stdlib.h>
#define LIM 30
char * s_gets(char * st, int n);
int main()
{
char number[LIM];
char * end;
long value;
puts("Enter a number (empty line to quit):");
while (s_gets(number, LIM) && number[0] != '\0')
{
value = strtol(number, &end, 10); /* base 10 */
printf("base 10 input, base 10 output: %ld, stopped at %s (%d)\n",
value, end, *end);
value = strtol(number, &end, 16); /* base 16 */
printf("base 16 input, base 10 output: %ld, stopped at %s (%d)\n",
value, end, *end);
puts("Next number:");
}
puts("Bye!\n");
return 0;
}
进制
base 10进制 输入输出都为10进制
base 16进制 输入输出都为16进制。
第一输入:
输入字符串10 相当于“10\0”
end 指向“\0”空字符,打印end,会显示一个空字符,%d输出*end的值为空字符的ASCII码值0。
第2输入:
10aaaa
10进制中:end为指向“aaaa\0”字符串的首地址“a”,打印字符串%s,结果为aaaa,%d为‘a’字符的ASCII码值。
16进制中:“a”为16进制数,故将10aaaa其转为十进制数为1092266,end为指向空字符的指针。
关键概念和本章小结
可以读一下,值得一读的小结:
2020-04-02 疫情还未结束,加油加油!!!!刚把得!!!!
大连
对fgets()函数的理解:
fgets函数读取n-1个字符,后加入‘\0’组成字符串形式。
对于stdin(键盘输入),在按下enter键之前,数据都存放于临时缓冲区,按下enter之后,会增加换行符,并把数据发送给fgets接收。但是如果n-1的数量小于输入数量,则其他字符仍会存在输入行。在下次fgets调用后,会接着读取n-1后面的字符。此时需要对其中的字符进行处理,采用while (getchar() != ‘\n’) continue;形式丢弃。
在while判断字符输入时可采用:(单独的null条件会使得程序一直运行)
while (fgets(words, STLEN, stdin) != NULL && words[0] != ‘\n’)
while (s_gets(words, STLEN, stdin) != NULL && words[0] != ‘\0’)
#include <stdio.h>
#define MAX 5
char * s_gets(char * st, int n);
int main(void)
{
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
double prize;
puts("Enter your first name:");
/*puts("Enter your last name:");
s_gets(last, MAX);
puts("Enter your prize money:");
scanf_s("%lf", &prize);
sprintf_s(formal, "%s, %-19s: $%6.2f\n", last, first, prize);*/
while (s_gets(first, MAX)!=NULL&&first[0]!='\0')
{
puts(first);
}
puts("bye");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
2020-04-03
课后习题值得做一做的!!!