C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2009, 03:26 AM   #1
Registered User
 
Join Date: May 2009
Location: Chennai, India
Posts: 17
Doubt in using % symbol under sprintf

Hi all,

I got a requirement of changing the display string in one of the application. It is something like changing "90 percentage CPU" to "90% CPU". First, i did the fix in one of the printf by doing

printf("%d\% CPU",per);

Then, i need the same changes under sprintf so i tried

sprintf(str,"\%%s","CPU");

But, when i tried to print the "str" i am getting the result as %s. So, i searched and found in some forum they suggested to use double % symbol like the following

sprintf(str,"%%%s","CPU");

With this change it is correctly printing %CPU. Can someon explain me how this intertpretation works under sprintf?

-BalaC-
cbalu is offline   Reply With Quote
Old 09-11-2009, 04:07 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 452
"\%" is wrong. It works here because it's escaped to "%", and since there is no valid character after the "%" printf will be sweet enough just to display %.
The solution is, as suggested, to replace all instances where you actually want to display "%" with "%%".
EVOEx is offline   Reply With Quote
Old 09-11-2009, 04:37 AM   #3
Registered User
 
Join Date: May 2009
Location: Chennai, India
Posts: 17
EVOEx,

Thanks for the clarification.

-BalaC-
cbalu is offline   Reply With Quote
Reply

Tags
sprintf

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
Stupid compiler errors ChrisEacrett C++ Programming 9 11-30-2003 05:44 PM
Linking error DockyD C++ Programming 10 01-20-2003 05:27 AM
<list> Unregistered C++ Programming 9 02-24-2002 04:07 PM


All times are GMT -6. The time now is 10:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22