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; }

30th Aug 2019, 10:41 AM
Mr. Nothing
Mr. Nothing - avatar
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.
30th Aug 2019, 12:46 PM
Nidhi
+ 2
Second printf in your code doesn't produce the output given in that print out.
30th Aug 2019, 12:31 PM
Nidhi
+ 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.
30th Aug 2019, 12:16 PM
Ipang
+ 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 👍
30th Aug 2019, 12:23 PM
Ipang
+ 1
And I found it on my college book
30th Aug 2019, 12:29 PM
Mr. Nothing
Mr. Nothing - avatar
+ 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.
30th Aug 2019, 12:37 PM
Ipang
+ 1
Guys, statement is wrong written by writer, Now I got it , It should be %4d,
30th Aug 2019, 12:38 PM
Mr. Nothing
Mr. Nothing - avatar
+ 1
Ipang one of my friend said it's wrong statement, 😅 ,
30th Aug 2019, 12:39 PM
Mr. Nothing
Mr. Nothing - avatar
+ 1
I wanted to tell you that also, but I need to be confirmed first, don't wanna go jumping into conclusion 😁
30th Aug 2019, 12:44 PM
Ipang
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
30th Aug 2019, 12:05 PM
Ipang
0
Ipang I'm confuse why % separated from (%d) specifier. ???? Is it valid , alone (d) specifier...
30th Aug 2019, 12:10 PM
Mr. Nothing
Mr. Nothing - avatar
30th Aug 2019, 12:15 PM
Mr. Nothing
Mr. Nothing - avatar
0
Ipang than why used (d) 🤔,
30th Aug 2019, 12:17 PM
Mr. Nothing
Mr. Nothing - avatar
0
And what is it, (d)? It's nor variable and not any symbol than what?
30th Aug 2019, 12:18 PM
Mr. Nothing
Mr. Nothing - avatar
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..
30th Aug 2019, 12:27 PM
Mr. Nothing
Mr. Nothing - avatar
0
So %4d what were I assume to be.
30th Aug 2019, 12:27 PM
Mr. Nothing
Mr. Nothing - avatar
0
But it's different so now I'm confused
30th Aug 2019, 12:28 PM
Mr. Nothing
Mr. Nothing - avatar
30th Aug 2019, 12:32 PM
Mr. Nothing
Mr. Nothing - avatar
0
? Is wrong statement written on book? 🤔 Tell me
30th Aug 2019, 12:34 PM
Mr. Nothing
Mr. Nothing - avatar