Connecting Tech Pros Worldwide Forums | Help | Site Map

printf of the "%d" string

Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86
#1: Jun 16 '08
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!

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Jun 16 '08

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]
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#3: Jun 17 '08

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
Member
 
Join Date: Mar 2007
Posts: 40
#4: Jun 26 '08

re: printf of the "%d" string


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

by using above two lines u can print %d itself
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#5: Jun 27 '08

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
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86
#6: Jun 27 '08

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,
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#7: Jun 30 '08

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
Member
 
Join Date: Jun 2008
Location: Igualada (Barcelona)
Posts: 86
#8: Jun 30 '08

re: printf of the "%d" string


I pass it to another function. Actually I was using sprintf() for that.
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#9: Jul 3 '08

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