当前位置:网站首页>Detailed explanation of printf() and scanf() functions of C language
Detailed explanation of printf() and scanf() functions of C language
2022-07-05 17:05:00 【Embedded universe】
List of articles
Preface
printf() 、scanf() I believe you are not unfamiliar with the two functions , however , Do you really understand these two functions , Through the study of this article , I believe you can realize that these two functions are different . This article will make a specific explanation for you through the combination of theory and code .
One 、printf() Function details
1. The function prototype
Standard format output function
int printf(const char *format, ...)
Parameters 1 : format Is a string , His content is the content displayed to the screen terminal . Its contents can be replaced by the following parameter values , The final formatted output to the screen terminal .
Parameters 2 : … It represents a variable length parameter , You can set several parameters of this function .
Return value : It returns an integer . The successful return is the number of characters printed on the screen , But does not include ‘\0’ , An error returns a negative number .
2. Detailed explanation of parameter specifier
format The specifier form is :%[ sign ][ Width ][. precision ][ Type length ] type
Except for the type , The others are optional .
sign :
sign | meaning |
---|---|
- | Align the standardized output left |
+ | Make the output result with a plus or minus sign (+ or -). Under normal circumstances, integers do not take +, Negative numbers will bring - |
Space | Add a space before the output result |
# | And 0 or x When used together with specifiers, it will add 0 or 0x, It is mainly convenient for viewing when printing . |
0 | Use... Before output 0 Fill instead of space . |
Width and accuracy :
Width : If the length of the number to be output is greater than the specified width , Then output as is ; If the length of the number to be output is less than the specified width , Then fill the space to the left .
precision : That is, the decimal places to be reserved , If the length of decimal places to be output is greater than the specified precision , The specified precision is retained by rounding ; If the decimal length to be output is less than the specified precision , It will make up for 0.
Examples are as follows
#include <stdio.h>
int main()
{
printf("%f\n", 13.5); // 13.500000
printf("%3.2f\n", 13.5); // 13.50
return 0;
}
In the above example , Output 13.5 If it is output in floating-point mode, it will retain the decimal by default 6 position , But after adding the width and precision control character output , It is output. 13.50.
Type length :
length | meaning |
---|---|
h | Parameters are interpreted as short integers |
l | Parameters are interpreted as growth shaping |
L | Parameters are interpreted as long double |
type :
type | meaning |
---|---|
d | Output as decimal signed integer |
o | Output in octal form |
x,X | Output in hexadecimal form |
u | Output as decimal unsigned integer |
f | Output in the form of single precision floating-point numbers |
lf | Output in the form of double precision floating-point numbers |
e,E | Output single in exponential form 、 Double precision real number |
g,G | With %f or %e Short output width in output single 、 Double precision real number |
c | Output in character form |
s | Output as a string |
p | Output variable address |
3. Code examples and error prone points
Source code :
#include <stdio.h>
int main()
{
char a = 'X';
char b[15] = "hello world";
int c = 100;
float d = 13.14;
double e = 161.123321;
int ret = 0;
printf("%c \n", a); // Output characters should be %c
printf("%s \n", b); // Output string to use %s
printf("%d \n", c); // Output shaping should use %d
printf("%f \n", d); // To output single precision floating-point numbers, use %f
printf("%lf \n", e); // To output double precision floating-point numbers, use %lf
ret = printf("hello world\n"); // Twelve characters will be output to the screen terminal , The return value is 12
printf("%d %d\n", ret, ret++); // First run ret++ expression , Running ret expression
return 0;
}
Running results :
X
hello world
100
13.140000
161.123321
hello world
13 12
Two 、scanf() Function details
1. The function prototype
Standard format input function
int scanf(const char *format, ...)
Parameters 1 : format Is a string , Accept the string from the screen terminal . The accepted content can be formatted into the following parameters .
Parameters 2 : … It represents a variable length parameter , You can set several parameters of this function .
Return value : It returns an integer . The successful return is the number of data items read , Error return EOF.
2. Detailed explanation of parameter specifier
format The specifier form is :%[*][ Width ][ Type length ] type
Except for the type , The others are optional .
Parameters | meaning |
---|---|
* | Lag character , Ignore the parameters read from the standard input stream , It is not assigned to the following parameters . |
Width | Specify the maximum number of characters that can be assigned . |
Type length | It's common for h or L, Such as hd,hu It will be stored in a signed or unsigned short integer |
type :
Convert specifiers | meaning |
---|---|
%c | Receive the input single character and pass it into the following parameters |
%d | Receive the input integer and pass in the following parameters |
%e、%E、%f、%F、%g、%G | Receive the input floating-point number and pass it to the following parameters |
%i | Receive the signed decimal input and pass it into the following parameters |
%o | Receive the input octal input to the following parameters |
%x,%X | Receive the hexadecimal input and pass in the following parameters |
%s | Receive the input string and pass in the following parameters . encounter Space 、 Line breaks and tabs end |
%u | Receive the input unsigned decimal and pass in the following parameters |
%p | Receive the input address and pass in the following parameters |
3. Code examples and error prone points
Source code :
#include <stdio.h>
int main()
{
char a = 'a';
int b = 13;
float c = 24;
int ret = 0;
printf("a = %c, b = %d, c = %f\n", a, b, c);
/*1. The variables received later must be received with address symbols */
/*2. The input content should be consistent with the content in double quotation marks except for the format controller */
ret = scanf("%c,%d,%f", &a, &b, &c);
printf("a = %c, b = %d, c = %f, ret = %d\n", a, b, c, ret);
return 0;
}
Running results :
a = a, b = 13, c = 24.000000
x,34,13.14
a = x, b = 34, c = 13.140000, ret = 3
边栏推荐
- Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
- Jarvis OJ shell traffic analysis
- Yarn common commands
- npm安装
- Binary tree related OJ problems
- Keras crash Guide
- Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
- Data verification before and after JSON to map -- custom UDF
- Allusions of King Xuan of Qi Dynasty
- Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
猜你喜欢
Solution of vant tabbar blocking content
How to install MySQL
【性能测试】jmeter+Grafana+influxdb部署实战
【729. 我的日程安排錶 I】
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
Jarvis OJ 简单网管协议
Learnopongl notes (I)
7.Scala类
Embedded UC (UNIX System Advanced Programming) -2
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
随机推荐
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
【剑指 Offer】66. 构建乘积数组
[brush title] goose factory shirt problem
DenseNet
飞桨EasyDL实操范例:工业零件划痕自动识别
What else do you not know about new map()
Jarvis OJ shell流量分析
【微信小程序】一文读懂小程序的生命周期和路由跳转
Wsl2.0 installation
Keras crash Guide
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
C# TCP如何设置心跳数据包,才显得优雅呢?
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
Embedded-c language-6
Jarvis OJ Webshell分析
Games101 notes (I)
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found