当前位置:网站首页>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
边栏推荐
- Bs-xx-042 implementation of personnel management system based on SSM
- Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?
- 干货!半监督预训练对话模型 SPACE
- [brush questions] effective Sudoku
- 通过proc接口调试内核代码
- Android privacy sandbox developer preview 3: privacy, security and personalized experience
- Jarvis OJ 简单网管协议
- 调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
- Error in composer installation: no composer lock file present.
- easyNmon使用汇总
猜你喜欢

PHP人才招聘系统开发 源代码 招聘网站源码二次开发
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details

How to uninstall MySQL cleanly

Jarvis OJ webshell analysis

Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city

浏览器渲染原理以及重排与重绘

【性能测试】jmeter+Grafana+influxdb部署实战

Copy mode DMA

Embedded -arm (bare board development) -1

Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
随机推荐
Facing new challenges and becoming a better self -- attacking technology er
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
浏览器渲染原理以及重排与重绘
Timestamp strtotime the day before or after the date
PHP talent recruitment system development source code recruitment website source code secondary development
Copy mode DMA
启牛商学院股票开户安全吗?靠谱吗?
Jarvis OJ shell流量分析
挖财股票开户安全吗?怎么开股票账户是安全?
Browser rendering principle and rearrangement and redrawing
[Web attack and Defense] WAF detection technology map
Little knowledge about C language (array and string)
Summary of PHP pseudo protocol of cisp-pte
C language to get program running time
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
【性能测试】jmeter+Grafana+influxdb部署实战
【testlink】TestLink1.9.18常见问题解决方法
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
PHP 严格模式
【beanshell】数据写入本地多种方法