第4章2014键盘输入和输出要点.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* scanf函数 scanf(%d,num); scanf 函数从标准输入(键盘)读取信息,按照格式描述把读入的信息转换为指定数据类型的数据,并把这些数据赋给指定的程序变量。 转换字符串 符号(附在读取的每个变量上)用于指明变量在内存中的位置 变量的名称 转换字符串 参数变量的类型 要求的实际输入 %d int 十进制数字序列 %ld long int 十进制数字序列 %f float 十进制数,可以有小数点及指数部分 %lf double 十进制数,可以有小数点及指数部分 * scanf函数 内存 float tax_rate; scanf ( %f , tax_rate); tax_rate 在内存中分配一块 32 位存储空间 存储空间使用名称“tax_rate”标识 提示用户输入一个值 输入的值存储在变量中(因为 scanf 提供此变量的地址) 12.234 * scanf函数使用 #include stdio.h int main( ) { double radius,high,vol; printf(请输入圆柱体底面积的半径和圆柱体的高: ); scanf(%lf%lf,radius,high); vol=3.14*radius*radius*high; printf(radius=%7.2f, high=%7.2f, vol=%7.2f\n, radius,high,vol); return 0; } 内存 radius high vol 5 10 785 请输入圆柱体底面积的半径和圆柱体的高: 5 10 radius= 5.00, high= 10.00, vol= 785.00 * scanf函数常见错误 常见错误 scanf(%d,%f\n,a,b); scanf(%d,%f,a,b); scanf(%7.2f,a); * scanf函数使用 #include stdio.h int main( ) { int a, b; printf(Please input a and b:); scanf(%d%d, a, b); printf(a=%d, b=%d, a+b=%d\n,a,b,a+b); return 0; } ↙ 以空格或TAB 键作为分隔符 * scanf函数使用 #include stdio.h int main( ) { int a, b; printf(Please input a and b:); scanf(%2d%2d, a, b); printf(a=%d, b=%d, a+b=%d\n,a,b,a+b); return 0; } Please input a and b: a=12, b=34, a+b = 46 1234↙ 遇宽度时结束 * scanf函数使用 #include stdio.h int main( ) { int a, b; printf(Please input a and b:); scanf(%d,%d, a, b); printf(a=%d, b=%d, a+b=%d\n,a,b,a+b); return 0; } Please input a and b: a=12, b=34, a+b = 46 12,34↙ 这里逗号需要原样输入 * scanf函数使用 #include stdio.h int main( ) { int a, b; printf(Please input a and b:); scanf(%2d%2d, a, b); printf(a=%d, b=%d, a+b=%d\n,a,b,a+b); return 0; } Please input a and b: a=12, b=3, a+b = 15 123a↙ 遇非法输入时结束 #include stdio.h main() { int a, b; printf(Please input a and b:); scanf(%2d%*2d%2d, a, b); printf(a=%d, b=%d, a+b=%d\n,a,b,a+b); } Please input a and b: a=12, b=56, a+b = 68 123456↙ 跳过一个输入项 输入数据的格式控制 * 输入数据的格式控制 #include stdio.h int main( ) { int a, b; sca

文档评论(0)

5201394 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档