How to print f% d literally

How to print f% literally

This is probably the simple answer. But I cannot find the answer.

Let's say I want:

printf( "printf(\"%d\", x );" );

      

I want the result to be literally: printf( "%d", x );

Is there a way to do this? If I just put it in the code I wrote above, the number ends up in% d, but I don't want anything to fit there. I just want literal% d.

+3


source to share


5 answers


Escape %

with another %

:



printf( "printf(\"%%d\", x );" );

      

+3


source


Okay, why aren't we doing some really safe programming practice here?



printf("%s", "printf(\"%d\", x );" );

      

+5


source


You can use %%

for literal percent.

+3


source


Use puts

or fputs

, this is the choice for unformatted text output

puts( "printf(\"%d\", x );");

      

to have it along with a trailing newline (which I think is a good idea) or

fputs( "printf(\"%d\", x );", stdout);

      

if you insist on no newline.

+3


source


Double tap %

to "run away":

printf( "printf(\"%%d\", x );" );
                   ^--

      

+1


source







All Articles