当前位置:网站首页>Detailed explanations of%*d,%.*s, etc. of [C language]: "recommended collection"
Detailed explanations of%*d,%.*s, etc. of [C language]: "recommended collection"
2022-07-28 11:24:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Catalog :
- 0. Preface 【 Lite version 】:
- Less time to see here :
- 1.【 Gentlemen and ladies stop !!!!】
- 0. Necessary introductory knowledge :
- 1. On the subject 1:scanf
- (1) %*d Shot dead :
- (2) 【%.*d 】 My teammate was shot :
- 2. On the subject 2:printf I won't talk about it , Ahead 【 Preface 】 It seems to be very clear .
0. Preface 【 Lite version 】:
In fact, there are many explanations about this on the Internet , But it always makes novices and even young veterans a little confused , For example, there is an explanation on the Internet that : Ignore …… What are you ignoring? You …
Less time to see here :
%\* and \*.* In the form of , Generally, there are only integers % And string %s Is more useful , But for your convenience , Here I list all the basic information :
Please note that :scanf and printf The situation in is different !
<1>
scanf:Be careful : stay scanf Only in Li %*d and %.*d meaningful , Other %*f,%*lf,%*c and %*s And so on are meaningless , The compiler will report an error . So don't Scribble .
(1) %*d( Only it makes sense : Ignore itself , And re match :)
int a=0,b=0,c=0;
scanf("%*d%d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=34,b=56,c=0
amount to : Ignore the first input 12( It itself ),
And will a Match the second number ,b And the third number ,
therefore c No matching input , so c It's still the original value 0.Below 2 For your reference :
/****************************************/
scanf("%d%*d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=12,b=56,c=0
amount to : Ignore the second input 34( It itself )
scanf("%d%d%*d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=12,b=34,c=0
amount to : Ignore the third input 56( It itself )(2) %.*d( Only it makes sense : Ignore %*d The next input of , And re match )
scanf("%.*d%d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=12,b=56,c=0
amount to : Ignore the second input 34( It's next )
therefore a Match the first input ,b Match the third input ,
The input in the middle is ignored , therefore c There is no match .Below 2 For your reference :
/****************************************/
scanf("%d%.*d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=12,b=34,c=0
amount to : Ignore the third input 56( It's next )
scanf("%d%d%.*d",&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
Input :12 34 56
Output :a=12,b=34,c=56
amount to : Ignore the fourth input ( It's next : air )
So the last one is meaningless ! And no .* equally .<2>
printf:Be careful : stay printf There are no restrictions ,%c,%d,%s,%f,%lf You can add * or .*, It makes sense . however , Their meanings are different :
(1) %*d( significance : It will definitely output all , Fill in the blanks according to the actual situation )
int a=123;
printf("a=%*d",2,a);
Output :a=123
( Output all 123, length >2, There is no need to fill in blanks )
It's the same as not specified !
printf("a=%*d",10,a);
Output :a= Space, space, space, space, space 123
( Fill forward 7 A space , Make up for the total 10 bits )(2) %.*d( significance : It will definitely output results , Fill in the front according to the actual situation 0)
printf("a=%.*d",2,a);
Output :a=123
( Output all 123, length >2, There is no need to mend 0)
It's the same as not specified !
printf("a=%.*d",10,a);
Output :a=0000000123
( Fill forward 7 individual 0, Make up for the total 10 bits )
Obviously, this situation is of little significance , Because we seldom need to make up 0.
So this kind of writing is basically not used .(3) %*f( significance : It will definitely output all , Fill in the blanks according to the actual situation ) Be careful ,%f The default is after the decimal point 6 digit : The decimal point is one place !
float a=12;
printf("a=%*f",3,a);
Output :a=123.000000
The default is after the decimal point 6 position . And it will definitely output all ,
therefore 10 position > designated 3 position , It's the same as not specified !
printf("a=%*f",13,a);
Output :a= Space... Space 123.000000
The default is after the decimal point 6 position . And it will definitely output all 10 position ,
be left over 3 position , So fill in the front 3 A space ( The decimal point is one place )(4) %.*f( significance : Specify the digits after the decimal point , Follow %.nf It's the same )
You'll see (5)%*s( significance : Also add a space in front according to the actual situation )
char *a="123456";
printf("a=%*s",5,a);
Output :a=123456; because 5<6, So it's the same as not specified
printf("a=%*s",10,a);
Output :a= Space... Space 123456(6)%.*s( significance : Specify to intercept the first few digits )
char *a="123456";
printf("a=%.*s",4,a);
Output :a=1234;
Intercept the front 4 digit
printf("a=%.*s",10,a);
Output :a=123456
Intercept the front 10 digit , Because only 6 position ,
So it's the same as not specified !1.【 Gentlemen and ladies stop !!!!】
If you have enough time, you can see my following wordiness :
0. Necessary introductory knowledge :
For you to eat the theory I will talk about later , Please read the following little knowledge you may never think about : 
What does that mean ? It means : You need it scanf Split some parameters into two parts to see ! Many beginners will forget to add addressing characters &, Because no one told him that the parameters in the second part used the address
Example 1:
scanf("%d%d%d");This is actually possible . Express : You can enter three values , But because the parameter in the second part is empty , That is, there is no address ,( Such as &, The pointer ), So the input value will not be saved in memory
Example 2:
int a,b,c;
scanf("",&a,&b,&c);It's ok . Express : You can type 0 Number , Because the parameters in the first part do not %, So there is no chance to input , How to save ? So behind it is a lonely .
Example 3:1+2
The above two examples add up to be really useful . So you often see this form : int a,b,c;
scanf("%d%d%d",&a,&b,&c);You are really familiar with this form ! Express : From left to right : first %d The input number is stored in the memory address a the second %d The input number is stored in the memory address b Third %d The input number is stored in the memory address c
1. On the subject 1:scanf
Use examples to reason :
(1) %*d Shot dead :

As shown in the figure , If it does %*, Will draw the next one % Team up illegally with her , Then follow &a matching .
however 《 National Marriage Law 》 Regulations , One person can only have one . therefore , It is stipulated here : The initiator of the illegal team %*d He was pulled out and shot by the report , Finally, leave the second and &a matching ( Because it matches from left to right ). therefore c There is no object .
in other words : Turned out to be a My wife was shot therefore : Turned out to be b My wife married a Turned out to be c My wife married b Eventually lead to c No wife ,c crying . therefore , You type 12 34 56 Then the output a,b,c, result : a=34,b=56,c=0( The default value is 0)
Excuse me, : The following form , Which and which illegal team ?
int a=0,b=0,c=0;
① ② ③
scanf("%d%*d%d",&a,&b,&c);answer :②③ Illegal team formation , because * After a Just to win over her last team !
(2) 【%.*d 】 My teammate was shot :
Bearing on the : The above example explains %*d The situation of , We know their secret illegal formation . namely :
int a=0,b=0,c=0;
① ② ③
scanf("%*d%d%d"):①② organize a team ,① Be shot
scanf("%d%*d%d"):②③ organize a team ,② Be shot
scanf("%d%d%*d"):③ Team up with the air ,③ Be shot Under the rev. : This situation :%.*d What's going on ? In fact, the truth is the same , It's illegal to form teams , But this time .* Not simple , So it won't be shot , But her teammate was shot :
int a=0,b=0,c=0;
① ② ③
scanf("%.*d%d%d"):①② organize a team ,② Be shot
scanf("%d%.*d%d"):②③ organize a team ,③ Be shot
scanf("%d%d%.*d"):③ Team up with the air , The air was shot So the third kind is equivalent to not adding .*, It's exactly equivalent to : scanf(“%d%d%d”); You can use either of these two . But wise you , You should choose the simplest second .
2. On the subject 2:printf I won't talk about it , Ahead 【 Preface 】 It seems to be very clear .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128285.html Link to the original text :https://javaforall.cn
边栏推荐
- 重新刷新你对Redis集群的理解
- Analysis of boot process of cortex-m4 and cortex-a7 kernel
- RHEL 6.4 安装svn和apache
- 使用共用体union及指针测试大小端
- What is WordPress
- 【MySQL从入门到精通】【高级篇】(九)InnoDB的B+树索引的注意事项
- Generation and use of Lib library files in keil and IAR
- Flutter教程之带有 GoRouter 的 Flutter Navigator 2.0,使用 go_router 包了解 Flutter 中的声明式路由机制(教程含源码)
- C language to convert float data into BCD data
- Relevant knowledge points of hash table
猜你喜欢

Two dimensional prefix and

Installation points and precautions of split angle probe

What is WordPress

Generation and use of Lib library files in keil and IAR

mysql还有哪些自带的函数呢?别到处找了,看这个就够了。

No swagger, what do I use?

精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”

GIS数据漫谈(五)— 地理坐标系统
Microsoft security team found an Austrian company that used windows Zero Day vulnerability to sell spyware

做数据分析,你还不懂RFM分析方法(模型)?
随机推荐
I use the applet container to improve the efficiency of mobile R & D by 5 times!
outlook突然变得很慢很卡怎么解决
go status.go 状态码定义
Array related knowledge points
几个数据库的相关概念
理解Oracle的几个概念
Why does MySQL sometimes choose the wrong index?
Reading these six books makes learning MySQL easier
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
keil和IAR中lib库文件的生成和使用
What functions does MySQL have? Don't look everywhere. Just look at this.
Nodejs: mongodb simple fuzzy + paging query instance
Software designers ask 20 questions before the exam, pay attention!!
Learn these analysis methods and models, and no longer have no ideas when encountering problems
Nodejs: return value of mongodb after successful insertion
[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03
什么是WordPress
Office2013 input mathematical formula above
使用共用体union及指针测试大小端
关于结构体指针函数的返回值传递给结构体指针的理解