当前位置:网站首页>scanf、printf的输入输出(格式控制符)
scanf、printf的输入输出(格式控制符)
2022-04-22 05:09:00 【编程小鹏】
一、使用scanf输入和printf输出
这两个函数是C语言中的
1、scanf的使用
格式:
scanf("格式控制",变量地址);
scanf("%d",&n);
常见数据类型变量的scanf格式符
| int | %d |
|---|---|
| long long | %lld |
| float | %f |
| char | %c |
| double | %lf |
| 字符串(char数组) | %s |
像13:45:20可以这样输入
scanf("%d:%d:%d",&h,&m,&s);
除了%c外,scanf对其他格式符的输入是以空白符(空格、Tab)为结束判断标志,字符数组使用%s读入的时候以空格跟换行作为读入结束的标志,%c是可以读入空格和换行的。
2、printf的使用
printf("格式控制",变量名称);
格式控制符只有double变为%f
如果想输出% ,只要在前面加上一个% \
二、常用输出格式
1、%md
可以使不足m位的int型变量以m位进行右对齐输出,高位用空格补齐,如果本身超过m位则保持原样。
#include<iostream>
using namespace std;
int main()
{
int a=123,b=123456;
printf("%5d\n",a);
printf("%5d\n",b);
return 0;
}
123
123456
2、%0md
与%md不同的是,当变量不足m位时,补足0.
#include<iostream>
using namespace std;
int main()
{
int a=123,b=123456;
printf("%5d\n",a);
printf("%5d\n",b);
printf("%05d\n",a);
return 0;
}
123
123456
00123
3、%.mf
可以让浮点数保留m位小数输出
#include<iostream>
using namespace std;
int main()
{
double d=3.14159;
printf("%.0f\n",d);
printf("%.1f\n",d);
printf("%.2f",d);
return 0;
}
3
3.1
3.14
三、使用getchar输入、putchar输出单个字符
可以识别换行符
char c1,c2;
c1=getchar();
c2=getchar();
putchar(c1);
putchar(c2);
版权声明
本文为[编程小鹏]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_57987156/article/details/124333401
边栏推荐
- [SQL Server accelerated path] database and table (II)
- Alibaba cloud performance test PTS new features in March
- Visio setting network topology
- Supplement to the usage of permute() function in torch (detailed process of matrix dimension change)
- Analyzing redis distributed locks from the perspective of source code
- 常见的测试方式
- Request and response objects
- 菜鸟的逆袭之路
- Chapter 7 select clause and sub query
- Chapter 3 basic SQL syntax
猜你喜欢

How to restrict Oracle sub query
Apple plans to expand children's information and communication security features to the UK and Canada

QBoxSet、QBoxPlotSeries

Enum enumeration type

Alibaba cloud performance test PTS new features in March

EMO-DB 数据集的 Speech 特征提取

Les racines et le contexte de Carina

Interpretation of the report Download | the future of database and the development trend and challenges of database in the 14th five year plan
Not sure whether it is a bug or a setting

Supplement to the usage of permute() function in torch (detailed process of matrix dimension change)
随机推荐
Esp32 vhci realizes ble broadcasting, which is so magical
All conditions that trigger epollin and epollout
MySQL double master and double slave + atlas data test
Final Cut Pro mosaics in multiple places of the video at the same time
Pydeck enables efficient visual rendering of millions of data points
Chapter 7 select clause and sub query
Jackson
Small bug -- hidden problem of Guide Package
MySQL encoding problem
How does docker access redis service 6379 port in the host
Summary of 2019 personal collection Framework Library
Considerations for importing idea package and calling its methods
TDD development mode and DDD development mode
Detailed explanation of Neptune w800 lighting (interruption) project
Pass files when openfeign is called
KNN prediction minimum case summary
Go array slice
2022.04.20 Huawei written examination
ObjectBox使用记录
QBoxSet、QBoxPlotSeries