Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!sun!amdcad!dvorak.amd.com!proton!tim From: tim@proton.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: print % in c Message-ID: <1991Feb27.020629.24624@dvorak.amd.com> Date: 27 Feb 91 02:06:29 GMT References: <61516@eerie.acsu.Buffalo.EDU> <31530035@hpcvia.CV.HP.COM> Sender: usenet@dvorak.amd.com (Usenet News) Reply-To: tim@amd.com (Tim Olson) Organization: Advanced Micro Devices, Austin, TX Lines: 32 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)