当前位置:网站首页>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
边栏推荐
- What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
- Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
- Learn these analysis methods and models, and no longer have no ideas when encountering problems
- DHCP experiment demonstration (Huawei switch device configuration)
- 构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
- CTF skill tree - file upload
- Inventory: exciting data visualization chart
- Installation points and precautions of split angle probe
- RHEL 6.4 installing SVN and Apache
- 大三下学期总结
猜你喜欢

Bc35 NB module at instruction development summary

Office2013 input mathematical formula above

No swagger, what do I use?

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult

用手机对电脑进行远程关机

Inventory: exciting data visualization chart

Do data analysis, do you still not understand RFM analysis method (model)?

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

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

What is WordPress
随机推荐
【MySQL从入门到精通】【高级篇】(十)MyISAM的索引方案&&索引的优缺点
大三下学期总结
表格数据处理软件,除了Excel还有什么?
重新刷新你对Redis集群的理解
Analysis of boot process of cortex-m4 and cortex-a7 kernel
JWT login authentication + token automatic renewal scheme, well written!
What is WordPress
A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
Understand several concepts of Oracle
Learn how to do e-commerce data analysis (with operation analysis index framework)
Encryption defect of icloud Keychain in Apple mobile phone
The use of C language linked list
Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?
做数据分析,你还不懂RFM分析方法(模型)?
Rongyun IM & RTC capabilities on new sites
Cortex-M内核管理全局中断的三种方式
机器学习强基计划0-5:为什么学习的本质是泛化能力?
No swagger, what do I use?
Three ways for Cortex-M kernel to manage global interrupts
ripro9.0修正升级版+WP两款美化包+稀有插件