最近在看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
第七章内容:https://blog.csdn.net/answerMack/article/details/103891048
主要介绍输入\输出函数(I/O函数):
输入函数:scanf()、getchar();
输出函数:printf()、putchar();
getchar()、putchar();
为单字符IO,每次只处理一个字符。也是绝大多数文本(即,普通文字)处理程序所用的核心方法。与头文件stdio.h相关联。不是真正的函数,被定义为供预处理器使用的宏。详解在16章。
C程序如何处理键盘输入,尤其是缓冲和标准输入文件的概念。
缓冲区
为什么要有缓冲?
1、把若干字符作为一个块传输比逐个发送字符节约时间
2、若打错字符,可直接通过键盘修正错误。当按下Enter时,传输的是正确的输入。
本书假设所有的输入都是缓冲输入
结束键盘输入
从文件开始学习如何阶数文件:
结果:
重定向和文件
Unix系统重定向
系统重定向输入:符号为“<”
win10系统重定向:
在win10的cmd命令行中执行:
打印出word.txt文件内容:
系统重定向输出:符号为“>”
结果:
覆盖掉之前的内容,替换了新的内容:
组合重定向:
重定向运算符原则:
符号“>>”和" | "
用程序直接打开文件
fopen等函数。具体介绍见后面几章。
fopen和fopen_s用法的比较
fopen 和 fopen_s
fopen用法: fp = fopen(filename,“w”)。
fopen_s用法:,须定义另外一个变量errno_t err,然后err = fopen_s(&fp,filename,“w”)。
返回值: fopen打开文件成功,返回文件指针(赋值给fp),打开失败则返回NULL值;
fopen_s打开文件成功返回0,失败返回非0。
在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,“w”)。而对于fopen_s来说,还得定义另外一个变量errno_t err,然后err = fopen_s(&fp,filename,“w”)。返回值的话,对于fopen来说,打开文件成功的话返回文件指针(赋值给fp),打开失败则返回NULL值;对于fopen_s来说,打开文件成功返回0,失败返回非0。
缓冲输入
混合数值和字符输入
程序
结果:
输入验证
// checking.c -- validating input
#include <stdio.h>
#include <stdbool.h>
// validate that input is an integer
long get_long(void);
// validate that range limits are valid
bool bad_limits(long begin, long end,
long low, long high);
// calculate the sum of the squares of the integers
// a through b
double sum_squares(long a, long b);
int main(void)
{
const long MIN = -10000000L; // lower limit to range
const long MAX = +10000000L; // upper limit to range
long start; // start of range
long stop; // end of range
double answer;
printf("This program computes the sum of the squares of "
"integers in a range.\nThe lower bound should not "
"be less than -10000000 and\nthe upper bound "
"should not be more than +10000000.\nEnter the "
"limits (enter 0 for both limits to quit):\n"
);
printf("lower limit: ");
start = get_long();
printf("upper limit: ");
stop = get_long();
while (start != 0 || stop != 0)
{
if (bad_limits(start, stop, MIN, MAX))
printf("Please try again.\n");
else
{
answer = sum_squares(start, stop);
printf("The sum of the squares of the integers ");
printf("from %ld to %ld is %g\n",
start, stop, answer);
}
printf("Enter the limits (enter 0 for both "
"limits to quit):\n");
printf("lower limit: ");
start = get_long();
printf("upper limit: ");
stop = get_long();
}
printf("Done.\n");
getchar();
return 0;
}
long get_long(void)
{
long input;
char ch;
while (scanf_s("%ld", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}
double sum_squares(long a, long b)
{
double total = 0;
long i;
for (i = a; i <= b; i++)
total += (double)i * (double)i;
return total;
}
bool bad_limits(long begin, long end,
long low, long high)
{
bool not_good = false;
if (begin > end)
{
printf("%ld isn't smaller than %ld.\n", begin, end);
not_good = true;
}
if (begin < low || end < low)
{
printf("Values must be %ld or greater.\n", low);
not_good = true;
}
if (begin > high || end > high)
{
printf("Values must be %ld or less.\n", high);
not_good = true;
}
return not_good;
}
菜单浏览
/* menuette.c -- menu techniques */
#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void);
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a': printf("Buy low, sell high.\n");
break;
case 'b': putchar('\a'); /* ANSI */
break;
case 'c': count();
break;
default: printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
void count(void)
{
int n, i;
printf("Count how far? Enter an integer:\n");
n = get_int();
for (i = 1; i <= n; i++)
printf("%d\n", i);
while (getchar() != '\n')
continue;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while ((ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
int get_int(void)
{
int input;
char ch;
while (scanf_s("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}