Connecting Tech Pros Worldwide Help | Site Map

printf of the "%d" string

  #1  
Old June 16th, 2008, 10:07 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86
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!
  #2  
Old June 16th, 2008, 10:36 AM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,117
Provided Answers: 6

re: printf of the "%d" string


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]
  #3  
Old June 17th, 2008, 02:03 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,257

re: printf of the "%d" string


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

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

re: printf of the "%d" string


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

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

re: printf of the "%d" string


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
  #6  
Old June 27th, 2008, 08:23 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86

re: printf of the "%d" string


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,
  #7  
Old June 30th, 2008, 02:11 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,257

re: printf of the "%d" string


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
  #8  
Old June 30th, 2008, 07:16 AM
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86

re: printf of the "%d" string


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

re: printf of the "%d" string


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the meaning of "char **option"? Omats.Z answers 5 October 16th, 2006 06:05 PM
Integer to "string" conversions bwaichu@yahoo.com answers 13 October 2nd, 2006 06:35 AM
The illusion of "portability" jacob navia answers 93 August 3rd, 2006 12:15 PM
The use of "free" Mars answers 13 November 14th, 2005 08:03 PM