fmsf
fmsf

Reputation: 37177

writing "%d" in a printf C

What is the correct syntax for this code: is it:

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

or

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

Or something else?

Upvotes: 2

Views: 2150

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283921

The second one. %d is defined by printf, not the C language, so you need to escape it with the printf %%, not a character escape.

A more complex example with a character escape sequence:

printf("printf(\"%%d\\n\",%s);\n", some_var);

Upvotes: 5

Jacob Relkin
Jacob Relkin

Reputation: 163318

The second one. In order to print a literal % you need to escape them by appending another %.

Upvotes: 3

Related Questions