#include<stdio.h>
char *str="char*str=%c%c%c;main(){printf(str,34,str,34);}";
void main()
{
printf(str,34,str,34);
}

gives the O/P

char *str="char*str=%c%c%c;main(){printf(str,34,str,34);}";

Can't say how??
No formatting string is used in printf() like %s or so.

The O/P you posted is wrong (the actual o/p may be slightly different from one computer to another because the second %c in the format string.)

char*str="∟";main(){printf(str,34,str,34);}

The formatting string is the first argument to printf() -- it does not have to be a string literal and can contain other characters besides %s, %c %f etc. printf() just displays the characters that are not followed immediately by '%' character.

Congratulations on finding a Quine.

Just substiute for readability

printf( "char*str=%c%c%c;main(){printf(str,34,str,34);}", // str,  
        34,
        "char*str=%c%c%c;main(){printf(str,34,str,34);}", // str,
        34 );

The first one contains 3 % conversions for the 3 params which follow
The second one is just a string which happens to contain % characters
The 'magic' is that it's the same string.

Congratulations on finding a Quine.

Just substiute for readability

printf( "char*str=%c%c%c;main(){printf(str,34,str,34);}", // str,  
        34,
        "char*str=%c%c%c;main(){printf(str,34,str,34);}", // str,
        34 );

The first one contains 3 % conversions for the 3 params which follow
The second one is just a string which happens to contain % characters
The 'magic' is that it's the same string.

Sorry, but I don't see any magic in it at all. The 3d argument is just used by printf() as an address of a string displayed as a character not as a string. He could have substituted "Hello World" there and the o/p would have been similar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.