Printing raw string literals

M

Mukesh_Singh_Nick

I am practicing printf formatting options. I want to print out a
string literal as a raw string as opposed to its special meaning. For
e.g I want to print out the "%f" part in:

printf("%f\n");

literally as "%f" rather than a zero value floating point/constant
double (0.000000).

I tried to escape the % sign but the compiler complained with a
warning about an illegal escape sequence.

I am using the Visual C++ 6.0 compiler.

A one-off solution could be to print it out as a string literal as:

char* s = 0;
s = "%f";
printf("%s\n", s);

But that'd work only for this format. I want a flexible solution since
I want to print out many of these formats as literals such as:

"%2.8f"
"%14.3f"
"%.9f"

etc.
 
M

Mukesh_Singh_Nick

I am practicing printf formatting options. I want to print out a
string literal as a raw string as opposed to its special meaning. For
e.g I want to print out the "%f" part in:

printf("%f\n");

literally as "%f" rather than a zero value floating point/constant
double (0.000000).

I tried to escape the % sign but the compiler complained with a
warning about an illegal escape sequence.

I am using the Visual C++ 6.0 compiler.

A one-off solution could be to print it out as a string literal as:

char* s = 0;
s = "%f";
printf("%s\n", s);

But that'd work only for this format. I want a flexible solution since
I want to print out many of these formats as literals such as:

"%2.8f"
"%14.3f"
"%.9f"

etc.




For instance, I could also use:

puts, or
putch, or
putchar, or
putche

but I want to only use printf because I am practicing. Suggestions,
please.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

I am practicing printf formatting options. I want to print out a
string literal as a raw string as opposed to its special meaning. For
e.g I want to print out the "%f" part in:

printf("%f\n");
printf("%%f\n");

literally as "%f" rather than a zero value floating point/constant
double (0.000000).

I tried to escape the % sign but the compiler complained with a
warning about an illegal escape sequence.

I am using the Visual C++ 6.0 compiler.

A one-off solution could be to print it out as a string literal as:

char* s = 0;
s = "%f";
printf("%s\n", s);

But that'd work only for this format. I want a flexible solution since
I want to print out many of these formats as literals such as:

"%2.8f"
"%14.3f"
"%.9f"

printf("%s\n", "%2.8f");
printf("%s\n", "%14.3f");
printf("%s\n", "%.9f");

would all work.
 
M

Mukesh_Singh_Nick

printf("%s\n", "%2.8f");
printf("%s\n", "%14.3f");
printf("%s\n", "%.9f");

would all work.- Hide quoted text -

- Show quoted text -



Thanks very much, friend. I almost forgot about the "%%" format
specifier. I had seen a C reference page mention it long ago. I can't
find it now. Can you please point me to a reference page that explains
it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top