c - What does %% do in printf? -


given following program in c,

#include <stdio.h>  int main()  {      printf(" \"books %% or %% apparels\"");      getchar();      return 0;  } 

the program prints books % or % apparels, know significance of %%, because looks unnecessary.

% indicates format escape sequence used formatting variables passed printf().

so have escape print % character.

http://en.cppreference.com/w/c/io/fprintf


if replace %% %, this

#include <stdio.h> int main (int argc, char *argv[]) {     printf(" \"books % or % apparels\"");     return 0; } 

there 2 escape sequences printf():

  1. "% o"

    [guesswork] not yields valid escape sequence printf() disregards , print is.

  2. "% a"

    printf() try float empty parameter list , print "hexadecimal floating point, lowercase" (%a, see above webpage). due c calling convention (disclaimer: i'm not expert on this), garbage memory used , explains result.

actually compiler (gcc 4.4.3) issued warnings both:

$ gcc a.c a.c: in function ‘main’: a.c:3: warning: unknown conversion type character ‘o’ in format a.c:3: warning: few arguments format 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -

java - Copying object fields -