学习总结

1、缓冲区分为完全缓冲区(fully buffered)I/O和行缓冲区(line-buffered)I/O。对完全缓冲输入来说,当缓冲区满的时候会被清空(缓冲区内容发送至其目的地)。这类型的缓冲区通常出现在文件输入中。对于行缓冲I/O来说,遇到一个换行字符时将被清空缓冲区,键盘输入是标准的行缓冲区。

2、EOF是C对文件结尾的一个标识,在stdio.h头文件中定义,#define EOF (-1)。在使用键盘输入时,可以通过Ctrl+D模拟EOF信号:

#include <stdio.h>
int main(){
int ch;
while((ch=getchar())!=EOF){
putchar(ch);
}
return ;
}

Abc[Enter]

abc

123[Ctrl+D]123

3、>和<都是重定向运算符,必须是可执行程序加文件,拿以上程序为例子:

执行:./test > abc

输入:

abcdefg[Enter]1234567[Enter]

执行:cat abc

输出:

abcdefg

1234567

4、除了以上重定向运算符还有>>运算符,该运算符可使您的一个现有文件的末尾追加数据。还有管道运算符(|),其实这些运算符都是Unix和Linux上的运算符。

执行:./test >> abc

输入:

hijklmn[Enter]

执行:cat abc

输出:

abcdefg

1234567

hijklmn

执行:./test | grep aaa

输入:aaabbbccc[Enter]dddeeefff[Enter]ggghhhaaa[Enter]

输出:

aaabbbccc

ggghhhaaa

5、在创建与用户对话的程序时,需要考虑到所有的边界问题,例如程序只需要用户输入a、b、c、d的时候,万一用户输入量其他的且一大串的字符会如何处理等等情况。还有程序同时需要getchar进行字符输入和使用scanf进行数字输入,这两个函数中的每一个都能很好的完成其工作,但它们不能很好地混合在一起,这是因为getchar读取每个字符,包括空格、制表符和换行符,而scanf在读取数字时则会跳过空格、制表符和换行符。

6、编程题(题8)

 #include <stdio.h>

 int getChoice(void);
int getFirst(void); int main(){
int ch,t,y;
float a,b;
int c;
char sa[],sb[]; ch=getChoice();
if(ch==){
return ;
}else{
printf("Enter first number:");
scanf("%s",sa);
while(sscanf(sa,"%f",&a)!=){
printf("%s is not an number.\nPlease enter a number.such as 2.5, -1.78E8, or 3:",sa);
scanf("%s",sa);
}
printf("Enter second number:");
scanf("%s",sb);
while(sscanf(sb,"%f",&b)!=||(ch==&&b==)){
if(ch==&&b==){
printf("Enter a number other than 0:");
}else{
printf("%s is not an number.\nPlease enter a number.such as 2.5, -1.78E8, or 3:",sb);
}
scanf("%s",sb);
}
} if(ch==){
printf("%s+%s=%.1f\n",sa,sb,a+b);
}else if(ch==){
printf("%s-%s=%.1f\n",sa,sb,a-b);
}else if(ch==){
printf("%s*%s=%s\n",sa,sb,a*b);
}else{
printf("%s/%s=%.1f\n",sa,sb,a/b);
} return ;
} int getChoice(void){
int ch;
printf("Enter the operation of your choice:\n");
printf("a. add b. subtract\n");
printf("c. multiply d.divide\n");
printf("q. quit\n");
ch=getFirst();
while(ch!=&&ch!=&&ch!=&&ch!=&&ch!=){
printf("Please enter a right choice:\n");
ch=getFirst();
} } int getFirst(void){
int ch;
ch=getchar();
while(getchar()!=)
return ;
return ch;
}

运行结果:

Enter the operation of your choice:

a. add          b. subtract

c. multiply     d.divide

q. quit

f

Please enter a right choice:

d

Enter first number:qqq

qqq is not an number.

Please enter a number.such as 2.5, -1.78E8, or 3:1

Enter second number:0

Enter a number other than 0:1

1/1=1.0

【C语言学习】《C Primer Plus》第8章 字符输入/输出和输入确认的更多相关文章

  1. C Primer Plus 第8章 字符输入/输出和验证输入 编程练习

    1. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch=getchar()) != EOF) ct++; pri ...

  2. C Primer Plus学习笔记(七)- 字符输入/输出和输入验证

    单字符 I/O:getchar() 和 putchar() getchar() 和 putchar() 每次只处理一个字符 getchar() 和 putchar() 都不是真正的函数,它们被定义为供 ...

  3. C Primer Plus 第4章 字符串和格式化输入/输出 编程练习

    1. #include <stdio.h> int main(void) { ]; ]; printf("请输入您的名字: "); scanf("%s&quo ...

  4. C语言学习书籍推荐《C Primer Plus(中文版)(第5版)》下载

    普拉塔 (Prata S.) (作者), 云巅工作室 (译者) <C Primer Plus(中文版)(第5版)>共17章,介绍了C语言的基础知识,包括数据类型.格式化输入输出.运算符.表 ...

  5. Go语言学习笔记四: 运算符

    Go语言学习笔记四: 运算符 这章知识好无聊呀,本来想跨过去,但没准有初学者要学,还是写写吧. 运算符种类 与你预期的一样,Go的特点就是啥都有,爱用哪个用哪个,所以市面上的运算符基本都有. 算术运算 ...

  6. 【C语言学习】《C Primer Plus》第1章 概览

    学习总结 1.C语言于1972年由贝尔实验室的Dennis Ritchie在与Ken Thompson一起设计UNIX操作系统的时候开发的.的的设计构想来源于Ken Thompson的B语言.Anyw ...

  7. linux 下C语言学习路线

    UNIX/Linux下C语言的学习路线.一.工具篇“公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工 ...

  8. 郝斌老师C语言学习笔记(一)

    在给变量分配内存时,很可能这段内存存在以前其他程序使用留下的值.当使用VC编译器,若编译器发现没有给变量赋值而使用,就会返回一个以“85”开头的很大的数字(此时该段内存中为一个垃圾数,为了避免出现较常 ...

  9. Unix和Linux下C语言学习指南

    转自:http://www.linuxdiyf.com/viewarticle.php?id=174074 Unix和Linux下C语言学习指南 引言 尽管 C 语言问世已近 30 年,但它的魅力仍未 ...

随机推荐

  1. Oracle 学习方法

    参考书籍: oracle实用教程 pdf 深入浅出Oracle: DBA入门.进阶与诊断案例.pdf Oracle 认证 Dba 认证: Oca  oracle 初级dba 认证(容易) Ocp  o ...

  2. Linux命令小结:crontab/netstat/iostat/sar

    crontab cron可以设定在指定的时间运行任务. 1.查看定时任务 [root@client1 ~]# crontab -l -u root */1 * * * * date >> ...

  3. c# 写着玩的,两个Task并发,一个写队列一个读队列的异常情况

    class Program { class TestEnqueue { static Queue<string> str = new Queue<string>(); publ ...

  4. 大漠绑定测试工具-VB6

    获取更新开始|版本:3.1652版 2016年12月27日|更新内容:1.取消自动更新错误的提示.\n\n友情提示:如网盘失效,请加QQ群(568073679)下载最新版|下载地址:http://ww ...

  5. C++面试中关于sizeof问题总结

    原文:http://blog.sina.com.cn/s/blog_7c983ca60100yfdv.html#SinaEditor_Temp_FontName (1)      sizeof是操作符 ...

  6. 51nod 1138 连续整数的和(数学公式)

    1138 连续整数的和 #include <iostream> #include <cmath> #include <cstdio> using namespace ...

  7. Windows 终端服务器授权 激活

      一.激活2003终端授权服务器 首先确认是否安装windows组件 添加删除程序——添加删除windows组件 终端服务器的安装就不介绍了,下面说一下激活授权服务器. 1)点击”开始”->” ...

  8. js 字符串格式化方法

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...

  9. shell编写mysql备份工具

    如需转载,请经本人同意. 这是之前写的一个备份脚本,调用的备份工具是xtrabackup 编写思路是:每周一全备份,备份后提取lSN号,对备份文件进行压缩,其余时候在LSN的基础上进行增量备份,并对3 ...

  10. MySQL Table is marked as crashed 解决方法

    MYSQL数据表出现问题,提示: ERROR 144 (HY000): Table './dpt/dpt_production' is marked as crashed and last (auto ...