Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old June 16th, 2008, 10:07 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Age: 29
Posts: 85
Default printf of the "%d" string

How to printf the string "%d" itself?

I wonder how should I escape the %, since I see no escape char for % (in http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.1.html)

Thanks!
Reply
  #2  
Old June 16th, 2008, 10:36 AM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 5,309
Default

There is not escape code for % because it doesn't need one. However there is a format code for % because in the context of a printf it does need one.

Read this: http://www.acm.uiuc.edu/webmonkeys/b...2.html#printf]
Reply
  #3  
Old June 17th, 2008, 02:03 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 29
Posts: 1,159
Default

To print % you should use % as the escape sequence.
Code will be
Expand|Select|Wrap|Line Numbers
  1. printf("%%");
  2.  
will print %

Raghu
Reply
  #4  
Old June 26th, 2008, 05:57 PM
Member
 
Join Date: Mar 2007
Posts: 39
Default

char a[3]="%d";
printf("%s",a);

by using above two lines u can print %d itself
Reply
  #5  
Old June 27th, 2008, 02:35 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 29
Posts: 1,159
Default

Quote:
Originally Posted by shivapadma
char a[3]="%d";
printf("%s",a);

by using above two lines u can print %d itself

I think the %% option would be much easier as u need not declare the local variable.

Raghuram
Reply
  #6  
Old June 27th, 2008, 08:23 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Age: 29
Posts: 85
Default

Yep,

In the end I did something like

Expand|Select|Wrap|Line Numbers
  1. printf("%%d");
It was meant to construct the string to pass later on to an fscanf() while reading a file.

Thanks,
Reply
  #7  
Old June 30th, 2008, 02:11 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 29
Posts: 1,159
Default

Quote:
Originally Posted by jorba101
Yep,

In the end I did something like

Expand|Select|Wrap|Line Numbers
  1. printf("%%d");
It was meant to construct the string to pass later on to an fscanf() while reading a file.

Thanks,


Hi,
You want to print % to console or have the % in a string to pass it to another function?

Raghu
Reply
  #8  
Old June 30th, 2008, 07:16 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Age: 29
Posts: 85
Default

I pass it to another function. Actually I was using sprintf() for that.
Reply
  #9  
Old July 3rd, 2008, 02:33 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 29
Posts: 1,159
Default

Quote:
Originally Posted by jorba101
I pass it to another function. Actually I was using sprintf() for that.
If u are using sprintf then u can follow the same idea

Expand|Select|Wrap|Line Numbers
  1. char arr[100];
  2. sprinf(arr,"%%");
  3.  
  4.  
Then u can pass arr to the function u are using.


Raghu
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles