当前位置:网站首页>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
边栏推荐
- WPF依赖属性(wpf 依赖属性)
- 大三下学期总结
- Sword finger offer 35. replication of complex linked list
- Inventory: 6 books teach you the necessary skills for career promotion
- 万字详解 Google Play 上架应用标准包格式 AAB
- Inventory: 144 free learning websites, the most complete collection of resources in the whole network
- Picture slide effect
- Tree shaking and DCE
- 用手机对电脑进行远程关机
- Understanding of the return value of the structure pointer function passed to the structure pointer
猜你喜欢

Installation points and precautions of split angle probe

Learn these analysis methods and models, and no longer have no ideas when encountering problems

低代码(aPaas)为什么最近又火了?

JS - 修改数组中对象的键名

表格数据处理软件,除了Excel还有什么?

Rongyun IM & RTC capabilities on new sites

leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】

使用 Terraform 在 AWS 上快速部署 MQTT 集群

开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开

小水滴2.0网站导航网模板
随机推荐
I use the applet container to improve the efficiency of mobile R & D by 5 times!
Clo********e: project management notes
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
Offsetof macro and container_ Of macro analysis details
Three ways for Cortex-M kernel to manage global interrupts
b2子主题/博客b2child子主题/开源源码
[MySQL from introduction to proficiency] [advanced chapter] (x) MyISAM's indexing scheme & advantages and disadvantages of indexing
WinForm generates random verification code
What is WordPress
Technology sharing | quick intercom integrated dispatching system
微软安全团队发现一家利用Windows零日漏洞销售间谍软件的奥地利公司
Game theory 1. Introduction (basic concepts of combination games, confrontation search, bash games, Nim games)
Remote shutdown of computer with mobile phone
Inventory: 6 books teach you the necessary skills for career promotion
本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
Why should coding and modulation be carried out before transmission
Use the common union and pointer to test the size end
leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
vim命令下显示行号[通俗易懂]
什么是WordPress