Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
#include<stdio.h>
int main()
{
   printf("%%%%%");
}

and the warning is
percent.c: In function ‘main’:
percent.c:5: warning: spurious trailing ‘%’ in format


and output is only "%%". Why not it is "%%%%%"?
Posted
Updated 5-Mar-11 23:09pm
v2
Comments
Richard MacCutchan 6-Mar-11 10:47am    
A simple solution:
printf("%s", "%%%%%");
remember that the first parameter to the printf function is the format string.
Sergey Alexandrovich Kryukov 6-Mar-11 14:11pm    
Not exactly simple. A simple one is puts("%%%%%"), in my answer :-)
--SA
Richard MacCutchan 7-Mar-11 4:06am    
But the issue is about the printf() function.
Sergey Alexandrovich Kryukov 7-Mar-11 14:55pm    
But it should not be.
Look, if everyone answered all questions directly, there would be almost no help.
It's like this:
-- Excuse me, do you know what time is it now?
-- Yes, I do. :-)
The answer is not the ultimate purpose; help is more imprtand.

Thank you.
--SA
Richard MacCutchan 7-Mar-11 16:37pm    
Well I agree your answer is informational, but the OP was specifically asking why his printf("%%%%%"); statement did not produce the expected results. Telling him/her to use puts() instead does not help with the original problem.

Because, in printf, the '%' character introduces the format specifier for outputed data:
printf("%d", myInteger);
or
printf("%s", myNullTerminatedString);
When you put "%%" in a printf string, you tell it to print a single '%' character.
When you put "%%%%%" it reads that as "Print '%'", then "Print '%'", then tries to find teh format specifier for the final '%' character. Since there is not further format string, it complains.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Mar-11 14:06pm    
Good explanation, my 5, but... I suspected OP simply needs puts("%%%%%"), my answer.
--SA
'%' in C/C++ is used for formatting the Printf Values.

Look here for full detail of how and what it will display when used: Section: Formatting other Types explains your example[^]

%% => display %
thus, %%%%% => display only 2 %. Consider % as a escape sequence for runtime.
 
Share this answer
 
Comments
CS2011 6-Mar-11 6:15am    
My 5 for link + explanation
Sergey Alexandrovich Kryukov 6-Mar-11 14:06pm    
Agree, my 5, but I suspected OP simply needs puts("%%%%%"), my answer.
--SA
Probably you need not formatted output, just "%%%%%". Use puts("%%%%%").

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900