0
Why % used before a= ,b=,c=,. instead of d in second statement
#include<stdio.h> Int main(void) { Int a=4000, b=200, c=15; Printf ("a=%d\nb=%d\nc=%d\n",a,b,c); Printf ("%a=4d\n%b=4d\n%c=4d\n",a,b,c); return 0; }
21 Réponses
+ 4
After % whichever alphabet comes first, is considered as format specifier and remaining are printed as it is, here "d" is treated as a normal character.
For example :
Printf("%d is a prime number", n) ;
Here "d" comes first after % so it'll be interpreted as format specifier and will be replaced with the value of n, and remaining characters (" is a prime number") will be printed as it is.
+ 2
Second printf in your code doesn't produce the output given in that print out.
+ 1
Aman
As it is now, your second call to printf has 2 valid specifiers, the %a and %c, the %b is in doubt.
I believe without a '%' prefix , (just 'd' alone) is not considered a format specifier, %d is indeed a valid specifier I guess.
+ 1
Aman
Can you confirm where did you find this snippet? I'd like to have a closer look please.
%d is a specifier for int, if that's what you ask for (I'm not sure). You can check the printf reference I linked on my previous response 👍
+ 1
And I found it on my college book
+ 1
Aman
I begin to wonder whether the example in the book was unverified, it makes more sense when the '%' is placed by the '4d' really (like '%4d').
I think we don't call a '%' in a format string as modulo, modulo operator (%) is used between operands (integral value), and not in a string. If I understood correctly.
+ 1
Guys, statement is wrong written by writer,
Now I got it ,
It should be %4d,
+ 1
Ipang one of my friend said it's wrong statement, 😅 ,
+ 1
I wanted to tell you that also, but I need to be confirmed first, don't wanna go jumping into conclusion 😁
0
%a is a format specifier used to display a value (floating point value) in hexadecimal notation.
%c is a format specifier used to display a value (integral value) as character.
Reference:
http://www.cplusplus.com/reference/cstdio/printf/
But I can't recall any use of %b (or whether it exists). I think the %b is causing a problem here, try to remove %b or add another % prefix so it looks like %%b.
At first I thought you possibly mistaken the placement of the %, and the line could've been probably:
printf ("a=%4d\nb=%4d\nc=%4d\n",a,b,c);
Which is a pretty valid format I guess : )
Hth, cmiiw
0
Ipang I'm confuse why % separated from (%d) specifier. ???? Is it valid , alone (d) specifier...
0
https://www.sololearn.com/post/142363/?ref=app
Look at here ,
0
Ipang than why used (d) 🤔,
0
And what is it, (d)? It's nor variable and not any symbol than what?
0
Yes %d is a specifier for int, and before today , I never see module spearted from %d specifier but ya I know 4 is a width length but I did not understand why module not written just before of d, because it should be as I know..
0
So %4d what were I assume to be.
0
But it's different so now I'm confused
0
nidhi
https://www.sololearn.com/post/142363/?ref=app
Look at here ,
0
? Is wrong statement written on book? 🤔 Tell me