* 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
您可能关注的文档
最近下载
- 《佳为软件资讯部培训教材》ppt模板课件.ppt VIP
- 《儿童文学》高职学前教育专业全套教学课件.pptx
- 班级管理第2版(下篇,共上中下3篇).pptx VIP
- 语言接触和语言演变课件.pptx VIP
- 最新版2026公文写作与处理模拟考试题库(含答案).docx
- C-J∕T 407-2012 城市轨道交通基于通信的列车自动控制系统技术要求(高清可复制).pdf VIP
- 2026年度山东劳动职业技术学院单招《数学》经典例题及答案详解(名师系列).docx VIP
- 周期天王周金涛人生就是一场康波.docx VIP
- 1177我的寒假生活PPT模板.pptx VIP
- GB50944-2013 防静电工程施工与质量验收规范.docx VIP
原创力文档

文档评论(0)