How does this program work

How does the following program work?
What's the use of 34 here?

1
2
3
4
5
6
7
8
  #include <stdio.h>
char *str = "charr* str = %c%s%c;main(){printf(str,34,str,34);}";
int main()
{
    printf(str,34,str,34);


}
%c and %s are format specifiers. They will be replaced by parameters finction is called with. %c is a character. As characters are actually integers you can easily pass 34 instead of '\"' what is actually done here.
Effectively all it's doing is something like this;
 
    printf("%c%s%c", '[', "elephant", ']');

which gives the output:
[elephant]


Printf uses the format string to determine how to interpret the parameters. In this case %c %s and %c meaning a single character, then a string, then another character.

Decimal 34 is the ASCII code for the double quote character " .
Topic archived. No new replies allowed.