By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

Printing raw string literals

P: n/a
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.

Jun 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 16, 3:31 pm, Mukesh_Singh_N...@yahoo.com wrote:
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.

Jun 16 '07 #2

P: n/a
Mu***************@yahoo.com wrote:
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.
Jun 16 '07 #3

P: n/a
On Jun 16, 3:36 pm, Harald van D k <true...@gmail.comwrote:
Mukesh_Singh_N...@yahoo.com wrote:
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.- 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?

Jun 16 '07 #4

P: n/a
On Jun 16, 3:45 pm, Mukesh_Singh_N...@yahoo.com wrote:
On Jun 16, 3:36 pm, Harald van D k <true...@gmail.comwrote:


Mukesh_Singh_N...@yahoo.com wrote:
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.- 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?- Hide quoted text -

- Show quoted text -
OK, I got it.

http://www.cppreference.com/stdio/printf.html

Thanks very much again.

Jun 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.