printf of the "%d" string 
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!
| 
June 16th, 2008, 10:36 AM
|  | 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] | 
June 17th, 2008, 02:03 AM
|  | 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
will print %
Raghu
| 
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
| 
June 27th, 2008, 02:35 AM
|  | 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
| 
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
It was meant to construct the string to pass later on to an fscanf() while reading a file.
Thanks,
| 
June 30th, 2008, 02:11 AM
|  | 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
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
| 
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.
| 
July 3rd, 2008, 02:33 AM
|  | 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 -
char arr[100];
-
sprinf(arr,"%%");
-
-
Then u can pass arr to the function u are using.
Raghu
|  | | | | /bytes/about
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 225,689 network members.
|