Doubt in using % symbol under sprintf

This is a discussion on Doubt in using % symbol under sprintf within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, I got a requirement of changing the display string in one of the application. It is something like ...

  1. #1
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43

    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-

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    "\%" 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 "%%".

  3. #3
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    EVOEx,

    Thanks for the clarification.

    -BalaC-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

Tags for this Thread


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