[comp.lang.c] print % in c

brianh@hpcvia.CV.HP.COM (brian_helterline) (02/26/91)

haozhou@acsu.buffalo.edu (Hao Zhou) writes:
>since % and \ are recognized as special characters in printf, how do
>you print out such kind special chars using printf?

Any character following a \ will be taken literally so just preceed
any "special" char with a \ so:

	printf( "This is a slash \\ and a precent sign \%\n" )

would produce: This is a slash \ and a percent sign %

rearl@geech.ai.mit.edu (Robert Earl) (02/26/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:

|	   To print special characters with printf(), precede the character
|   with a \ character.  Example:
|
|	   printf("This a percent sign: \%\n");
|	   printf("This is a backslash: \\\n");

The first example doesn't work-- you're still passing a `%' character
to printf(), in fact you're passing it a format character with no
corresponding argument, which really causes it to barf.

As the manual says, passing a double `%' to printf means to print a
literal `%' character.  And you must escape backslashes in any string
constant in source code, not just printf arguments.

--robert

gwyn@smoke.brl.mil (Doug Gwyn) (02/26/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>	To print special characters with printf(), precede the character
>with a \ character.  Example:
>	printf("This a percent sign: \%\n");

That produces undefined behavior.

To escape a literal percent character in a *printf()/*scanf() format string,
specify two adjacent percent characters.

drh@duke.cs.duke.edu (D. Richard Hipp) (02/26/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu>
gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>	To print special characters with printf(), precede the character
>with a \ character.  Example:
>
>	printf("This a percent sign: \%\n");
>	printf("This is a backslash: \\\n");

Nope.  John is wrong here.  To print a % using printf, put two %'s in
a row, like this:

        printf("This is a percent sign: %%\n");

peter@ficc.ferranti.com (Peter da Silva) (02/26/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
> 	To print special characters with printf(), precede the character
> with a \ character.  Example:

Backslash works on stuff interpreted by the compiler. % isn't one of them.

> 	printf("This a percent sign: \%\n");
No:	printf("This is a percent sign: %%\n");
-- 
Peter da Silva.  `-_-'  peter@ferranti.com
+1 713 274 5180.  'U`  "Have you hugged your wolf today?"

dean@usenet.INS.CWRU.Edu (Dean Cookson) (02/26/91)

In article <31530035@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
>Any character following a \ will be taken literally so just preceed
>any "special" char with a \ so:
>
>	printf( "This is a slash \\ and a precent sign \%\n" )
>
>would produce: This is a slash \ and a percent sign %
Ahhh no.

What you want to do is:
printf("%%\n");

Which will print:
%


Is in K&R.

Dean


-- 
| Dean Cookson 	(Opinions?  What opinions??)	dean@po.cwru.edu	      |
| Information Network Services   		DoD #207 AMA #573534	      |
| Case Western Reserve U. Cleveland, Oh 44106	1981 Honda CB750C	      |
| 216-754-1834 (H) 216-368-2928 (W)	 	1988 Bianchi Limited	      |

rcd@ico.isc.com (Dick Dunn) (02/26/91)

brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
...
> 	printf( "This is a slash \\ and a precent sign \%\n" )
> 
> would produce: This is a slash \ and a percent sign %

No, it wouldn't, and this is the second person to blow it in a followup.

Look, folks, if you're going to give completely wrong, untested answers to
elementary questions, could you at least send them via email instead of
posting them???
-- 
Dick Dunn     rcd@ico.isc.com -or- ico!rcd       Boulder, CO   (303)449-2870
   ...But is it art?

awm@shamash.cdc.com (Allan Magnuson) (02/26/91)

In article <1991Feb25.232900.18406@usenet.ins.cwru.edu> dean@po.CWRU.Edu writes:
>In article <31530035@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
>>Any character following a \ will be taken literally so just preceed
>>any "special" char with a \ so:
>>
>>	printf( "This is a slash \\ and a precent sign \%\n" )
>>
>>would produce: This is a slash \ and a percent sign %
>Ahhh no.
>

Well to waste more bandwith....I compiled, linked, linted and tested this one.

#include <stdio.h>

main()
        {
        printf ("This is a percent sign %c\n",'%');
        printf ("This is a percent sign %%\n");
        printf ("This is a percent sign %s\n","%");
        printf ("This is a percent sign %c\n",'\045');  /* ASCII dependent */
        printf ("This is a percent sign %c\n",0x25);    /* ASCII dependent */
        }

Eric Swildens
awm@shamash.cdc.com   ( Still using Al's account ;-) )

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (02/26/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes:
> 	To print special characters with printf(), precede the character
> with a \ character.  Example:

> 	printf("This a percent sign: \%\n");
> 	printf("This is a backslash: \\\n");

Did you *try* this?  Backslash is handled by one of the compiler
phases.  The string "This is a percent sign \%\n" turns into the
characters <T,h,i,s, ,i,s, ,a, ,p,e,r,c,e,n,t, ,s,i,g,n, ,%,NEWLINE>
and by the time printf() sees it there is nothing special about the
percent sign.  To get a percent sign from printf(), use two % signs:
	printf("The original answer was 100%% wrong.\n");
-- 
The purpose of advertising is to destroy the freedom of the market.

browns@iccgcc.decnet.ab.com (Stan Brown) (02/27/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes:
> 	To print special characters with printf(), precede the character
> with a \ character.  Example:
> 
> 	printf("This a percent sign: \%\n");
> 	printf("This is a backslash: \\\n");

The first of these is bad advice, and comes from mixing compile-time
treatment of strings with run-time treatment by printf( ) of its first
argument. 

An easy counterexample:
        printf("This is a number without percent sign: \%d\n", 1568);
will print:
        This is a number without percent sign: 1568
And yes, I've tried it (Microsoft C 5.1).

Explanation:

The backslash in a string has special meaning at compile time, to tell
the compiler that the next character is special.  The percent sign has
no special meaning at compile time.

So the two strings given above actually contain characters ending in
          n, :, space, %, newline, ASCII 0
and       h, :, space, \, newline, ASCII 0

The percent sign has special meaning at run time, in a printf string, to
introduce a conversion specification.  The backslash has no special
meaning to printf.  The standard states (page 135) that the complete
conversion specification to write a % shall be %%.

In the next paragraph on page 135, the standard states that if a
conversion specification is invalid, the behavior is undefined.  Newline
is not a valid conversion specification.  So a compiler is within its
rights to produce % from % followed by an invalid conversion
specification, but you can't count on it.   (BTW Microsoft C 5.1 prints
nothing after the colon and space.)

Maybe this belongs in an FAQ?  I haven't seen it often on the net,
but it seems a common misunderstanding among programmers.

My opinions are mine:  I don't speak for any other person or company.
                                   email: browns@iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA    +1 216 371 0043

pjh@tech.uucp (Pete Holsberg) (02/27/91)

In article <1991Feb26.004336.5179@ico.isc.com> rcd@ico.isc.com (Dick Dunn) writes:
=Look, folks, if you're going to give completely wrong, untested answers to
=elementary questions, could you at least send them via email instead of
=posting them???

But then the recipient would not know that they were wrong!  This way,
people who know the correct answers can post them.

What you might have said is, "Please post incorrect answers and email
correct ones."  ;-)

Pete
-- 
Prof. Peter J. Holsberg      Mercer County Community College
Voice: 609-586-4800          Engineering Technology, Computers and Math
UUCP:...!princeton!mccc!pjh  1200 Old Trenton Road, Trenton, NJ 08690
Internet: pjh@mccc.edu	     Trenton Computer Festival -- 4/20-21/91

garry@ceco.ceco.com (Garry Garrett) (02/27/91)

In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes:
> 
> 	To print special characters with printf(), precede the character
> with a \ character.  Example:
> 
> 	printf("This a percent sign: \%\n");
> 	printf("This is a backslash: \\\n");
> 
> 
> ---
> John Gordon
> Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
>           gordon@cerl.cecer.army.mil       #include <clever_saying.h>

	Well, I don't think that \% is univeral.  (perhaps in later versions
of unix).  I know that 

	printf("This a percent sign: %%\n");

will work.  In general, when a symbol has been used as a special symbol,
you can use that symbol by repeating it twice (%% or \\).  There are alot
of \_ symbols, but at least on my system \% is not one of them.

Garry Garrett

tim@proton.amd.com (Tim Olson) (02/27/91)

In article <31530035@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
| haozhou@acsu.buffalo.edu (Hao Zhou) writes:
| >since % and \ are recognized as special characters in printf, how do
| >you print out such kind special chars using printf?
| 
| Any character following a \ will be taken literally so just preceed
| any "special" char with a \ so:
| 
| 	printf( "This is a slash \\ and a precent sign \%\n" )
| 
| would produce: This is a slash \ and a percent sign %

No, it would produce: This is a slash \ and a percent sign
				     [note missing % !!!]  ^

The real answer is that '\' is a special escape character for
character constants and strings in C, and it is processed at
compile/assemble time.  It can be used to represent frequently used
values (e.g. \t = tab, \r = return, \n = newline, \\ = \), as well any
character by following the backslash with an octal constant.

printf(), on the other hand, uses '%' as its escape character, and
interprets it at runtime.  To get printf to print a '%' character, you
need to escape it with another %:

	printf("This is a percent sign: %%\n");


--
	-- Tim Olson
	Advanced Micro Devices
	(tim@amd.com)

richard@iesd.auc.dk (Richard Flamsholt S0rensen) (02/27/91)

>>>>> On 26 Feb 91 09:50:19 GMT, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) said:
Richard> In article <1991Feb25.180600.5004@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes:
> 	To print special characters with printf(), precede the character
> with a \ character.  Example:

> 	printf("This a percent sign: \%\n");
> 	printf("This is a backslash: \\\n");

Richard> Did you *try* this?  Backslash is handled by one of the compiler
Richard> phases.  The string "This is a percent sign \%\n" turns into the
Richard> characters <T,h,i,s, ,i,s, ,a, ,p,e,r,c,e,n,t, ,s,i,g,n, ,%,NEWLINE>

  Did *you* try this? Backslash doesn't by any means "protect the next
char" - it handles a limited number of predefined escape sequences.

--
/Richard Flamsholt
richard@iesd.auc.dk

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (02/28/91)

In article <RICHARD.91Feb27110730@laplace.iesd.auc.dk>, richard@iesd.auc.dk (Richard Flamsholt S0rensen) writes:
> > 	printf("This a percent sign: \%\n");
> > 	printf("This is a backslash: \\\n");
> 
> Richard> Did you *try* this?  Backslash is handled by one of the compiler
> Richard> phases.  The string "This is a percent sign \%\n" turns into the
> Richard> characters <T,h,i,s, ,i,s, ,a, ,p,e,r,c,e,n,t, ,s,i,g,n, ,%,NEWLINE>
> 
>   Did *you* try this? Backslash doesn't by any means "protect the next
> char" - it handles a limited number of predefined escape sequences.

Yes I ***DID*** try it.  Nowhere did I state or imply that backslash
"protects the next char".  The correct statement is
	"The effect of backslash is *PARTIALLY* defined in the ANSI C
	standard.  \% is *NOT* defined.  However, UNIX compilers have
	historically treated \@ just like @ whenever @ is not one of
	the characters whose effect is officially defined.  So while
	a compiler may do anything at all when it comes across  \%,
	it is most likely to treat it just like %."
There.  Will that do?  (What's more, I tried it under several compilers
before I posted.  Sadly, all were pcc-based.)

-- 
The purpose of advertising is to destroy the freedom of the market.

dean@usenet.INS.CWRU.Edu (Dean Cookson) (02/28/91)

In article <4843@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
[Stuff Deleted]
>	So while
>	a compiler may do anything at all when it comes across  \%,
>	it is most likely to treat it just like %."
>There.  Will that do?  (What's more, I tried it under several compilers
>before I posted.  Sadly, all were pcc-based.)
>
Nope, sorry but it won't.  You had me up to the "most likely" part.  The 
behavior is undefined. (See section A.2.5.2 Character Constants of the
Ref. Manual in K&R 2)  What the compiler is most likely to do is something
random. (ie your pcc based compilers like it, gcc gives a warning then
doesn't print it.  Sun cc gives no warning, then doesn't print it)

Dean
-- 
| Dean Cookson 	(Opinions?  What opinions??)	dean@po.cwru.edu	      |
| Information Network Services   		DoD #207 AMA #573534	      |
| Case Western Reserve U. Cleveland, Oh 44106	1981 Honda CB750C	      |
| 216-754-1834 (H) 216-368-2928 (W)	 	1988 Bianchi Limited	      |

sef@kithrup.COM (Sean Eric Fagan) (02/28/91)

In article <1991Feb27.020629.24624@dvorak.amd.com> tim@amd.com (Tim Olson) writes:
>The real answer is that '\' is a special escape character for
>character constants and strings in C, and it is processed at
>compile/assemble time.  It can be used to represent frequently used
>values (e.g. \t = tab, \r = return, \n = newline, \\ = \), as well any
>character by following the backslash with an octal constant.

Heh.  Moderately amusing story:  a friend of mine had worked extensively on
a Small-C compiler, to the point where it was not quite small-C anymore.
Anyway, the compiler treated "\%" as "\%", and his version of printf treated
that as an escape for '%' (i.e., equivalent to '%%').

He didn't believe me when I told him he was wrong... 8-)

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef@kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.

sef@kithrup.COM (Sean Eric Fagan) (02/28/91)

In article <15329@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>To escape a literal percent character in a *printf()/*scanf() format string,
>specify two adjacent percent characters.

Yes, but we can't expect people to actually *think* and/or read manuals, can
we?

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef@kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.

newsuser@oliver.SUBLINK.ORG (Ugo Cei) (03/02/91)

dean@usenet.INS.CWRU.Edu (Dean Cookson) writes:

>In article <4843@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>[Stuff Deleted]
>>	So while
>>	a compiler may do anything at all when it comes across  \%,
>>	it is most likely to treat it just like %."
>>There.  Will that do?  (What's more, I tried it under several compilers
>>before I posted.  Sadly, all were pcc-based.)
>>
>Nope, sorry but it won't.  You had me up to the "most likely" part.  The 
>behavior is undefined. (See section A.2.5.2 Character Constants of the
>Ref. Manual in K&R 2)  What the compiler is most likely to do is something
>random. (ie your pcc based compilers like it, gcc gives a warning then
>doesn't print it.  Sun cc gives no warning, then doesn't print it)

I think Richard is right on this one. What he was saying is that the
*compiler* is likely to treat "\%" as "%". Obviously, when *printf*
sees it, it does not know what to do and so it is likely to print
nothing at all. This is true under SCO's rcc, MSC 5.1 and gcc 1.39
(which indeed gives a warning : "unknown escape sequence `\%'").
The following line of code will print the `%' under any of the three
mentioned compilers. (I presume Sun cc will do the same.)

	fputs("This is a percent sign: \%\n", stdout);

However, I certainly don't think that this is a good and/or portable
thing to do.
-- 
**************** | Ugo Cei            | SUBLINK: newsuser@oliver.sublink.org
*    OLIVER    * | Via Colombo 7      | INTERNET:  cei@ipvvis.unipv.it
**************** | 27100 Pavia ITALY  |     "Real Programs Dump Core"

dean@usenet.INS.CWRU.Edu (Dean Cookson) (03/05/91)

In article <875@oliver.SUBLINK.ORG> newsuser@oliver.SUBLINK.ORG (Ugo Cei) writes:
>I think Richard is right on this one. What he was saying is that the
>*compiler* is likely to treat "\%" as "%". Obviously, when *printf*
>sees it, it does not know what to do and so it is likely to print
>nothing at all. This is true under SCO's rcc, MSC 5.1 and gcc 1.39
>(which indeed gives a warning : "unknown escape sequence `\%'").
>The following line of code will print the `%' under any of the three
>mentioned compilers. (I presume Sun cc will do the same.)
>
Ok, I'll give ya that one.  :-)

Dean
-- 
| Dean Cookson 	(Opinions?  What opinions??)	dean@po.cwru.edu	      |
| Information Network Services   		DoD #207 AMA #573534	      |
| Case Western Reserve U. Cleveland, Oh 44106	1981 Honda CB750C	      |
| 216-754-1834 (H) 216-368-2928 (W)	 	1988 Bianchi Limited	      |