Thread: vsprintf ommit literal percent character

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    vsprintf ommit literal percent character

    Hello, recently I have had a problem while trying to makeing a function that sets some formated text to a Status control on a window based application, the problem is basically that vsprintf is ommiting any percentaje (%) character that I want to use as a literal character (for example, while searching something I need to inform the user in a way like that: "Reached 25% of files"); first I thought that was a window thing, so I tested in console mode and I get the same result. That's the testing code I used:

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    
    void apply_text(char *data, ...) {
        
        va_list args;
        char output_text_buffer[100];
        
        if(data) {
            va_start(args, data);
            vsprintf(output_text_buffer, data, args);
            va_end(args);
            
            //testing result
            printf("%s", output_text_buffer);
        }
    }
    
    
    int main() {
        
        apply_text("Reach 25% of files");
        
        getchar();
        return 0;
    }
    Even if I use a backslash before the %, or if I use a %c to print the ASCII 37 it is ommiting the percentaje, the result is "Reach 25 of files".

    How can I solve that?
    Thank's in advance

    Niara

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    % is the escape character for all the print type functions. Notice how you use "%s" to print a string. You need to use two percent signs to get a literal percent "... 25%% ...".

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need two percent signs in a row %% in a format string to get a percent sign.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Yeah it works, thanks both anduril462 and oogabooga for your responses, I can go on now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 50 percent for this homework?
    By a.mlw.walker in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 05:15 PM
  2. variable arguments and vsprintf
    By stephen0731 in forum C Programming
    Replies: 1
    Last Post: 05-31-2008, 03:20 PM
  3. percent transparentcy?
    By kryptkat in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2006, 05:49 PM
  4. Percent of each random number
    By CrackerJack in forum Windows Programming
    Replies: 10
    Last Post: 11-10-2004, 01:02 PM
  5. vsprintf overrun -> _vscprintf?
    By Mox in forum C Programming
    Replies: 5
    Last Post: 07-14-2003, 01:14 PM