print % in c

Ugo Cei newsuser at oliver.SUBLINK.ORG
Sat Mar 2 20:55:41 AEST 1991


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

>In article <4843 at goanna.cs.rmit.oz.au> ok at 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 at oliver.sublink.org
*    OLIVER    * | Via Colombo 7      | INTERNET:  cei at ipvvis.unipv.it
**************** | 27100 Pavia ITALY  |     "Real Programs Dump Core"



More information about the Comp.lang.c mailing list