当前位置:网站首页>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
边栏推荐
猜你喜欢

国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度

Jarvis OJ shell traffic analysis

Jarvis OJ Flag

WSL2.0安装

DenseNet

Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

Embedded -arm (bare board development) -2

File operation --i/o

调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%

Jarvis OJ 远程登录协议
随机推荐
【Web攻防】WAF检测技术图谱
How to install MySQL
Allusions of King Xuan of Qi Dynasty
C# TCP如何设置心跳数据包,才显得优雅呢?
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
Application of threshold homomorphic encryption in privacy Computing: Interpretation
Data verification before and after JSON to map -- custom UDF
ECU introduction
npm安装
[61dctf]fm
启牛商学院股票开户安全吗?靠谱吗?
Jarvis OJ 简单网管协议
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
Practical example of propeller easydl: automatic scratch recognition of industrial parts
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
齐宣王典故
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
Embedded-c Language-1
[Jianzhi offer] 63 Maximum profit of stock
Cs231n notes (bottom) - applicable to 0 Foundation