当前位置:网站首页>scanf和printf格式问题
scanf和printf格式问题
2022-07-22 10:40:00 【LH2HA3】
一.scanf函数
1.该函数可以获取任意数据类型的输入,但是必须指定输入格式:
%c | 读入一个字符 |
%s | 读入一个字符串 |
%d | 读入一个十进制数 |
%[^\n] | 读入一组字符串 |
%f | 读入一个单精度浮点数 |
%lf | 读入一个双精度浮点数 |
%0 | 读入一个八进制数 |
%x | 读入一个十六进制数 |
%e | 读入一个科学计数法的数 |
如:scanf("%d%d",&num,&num); 这表示连续读入两个十进制整数(你还可以读入更多)
在这里‘&’表示取地址符,表示将输入的数值写入num的地址内存地址。
说明:所有的变量多需要保存在内存地址中,在输入时在变量前加上'&'.但是特别注意字符串变量名就代表一个地址内存,因此前面并不需要添加'&'.
如: scanf("%s%c",name,&c);
3.按格式输入
- 输入值以“,”逗号分隔:scanf("%d,%d",&num1,&num2);
- 输入值以“:”冒号分隔:scanf("%d:%d",&num1,&num2);
- 输入值以“/”斜杠分隔:scanf("%d/%d",&num1,&num2);
二.printf()函数
该函数是用来显示字符、字符串、数值等等。
在printf()函数中可以使用scanf()函数的格式控制符,还可以使用转义字符。
\n | 换行,将光标移到下一行 |
\t | 水平制表符 |
\r | 回车 |
\\ | 反斜杠符(\) |
\' | 单引号' |
\'' | 双引号 |
- 给出指定输出宽度:printf("%5d",num); (右对齐)预留5个宽度来输出一个整数。printf("%-5d",num);(左对齐)预留5个宽度来输出一个整数。
- 对浮点数,输入指定小数点后几位:printf("%.5f",num1);输出小数点后五位数。printf("%5.2f",num2); 预留5个宽度来输出一个保留小数点后两位的浮点数。
说明:在c语言中不只scanf()和printf()函数用于输入输出,输入输出函数还有——getchar()和putchar();gets()和puts();
边栏推荐
猜你喜欢
【FPGA】:ip核--Divider(除法器)
Pre training weekly 39: deep model, prompt learning
Pytoch sets different learning rates at different levels
Latex compiles and reports errors in vscode `recipe terminated with error Retry building the project.
自动电流镜布局 (ACML) 工具
有源和无源设备的共质心布局:回顾和未来之路
Aminer paper recommendation
Rag summary
Wiring ability of twisted common centroid capacitor array under signal coupling constraints
Analysis of class parameters in pytoch, source code analysis of in class member function.Parameters (), acquisition of parameter set, registration and assignment of parameters, source code analysis
随机推荐
Deformable Detr paper accuracy, and analyze the network model structure
Lingo 基本使用
Chen Danqi: I hope girls can get more opportunities, and the gap between boys and girls will gradually disappear
Advent of code 2020 -- 登机座位问题
Dense passage retrieval for open domain question answering notes
1038 recover the smallest number (30 points)
Simulate QQ login interface
Vim学习之旅
Vscode关闭自动更新
1064 Complete Binary Search Tree (30 分)
冒泡排序
正则表达式学习笔记
希尔排序(最小增量排序)
Process fork
1049 counting ones (30 points)
电荷分级DAC中二元加权电容器的寄生感知大小和详细路由
Causal learning weekly, issue 10: introduction to the latest causal discovery related papers in iclr2022
VIM learning journey
LeetCode - 整数反转
7-1 Fake News (20 分)