Sunday, July 12, 2009

Any Idea What this C Code is all about??

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;stdlib.h%26gt;





void main()


{


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


printf(a,34,a,34);


getch();


}








Could someone explain the Logic behind this??

Any Idea What this C Code is all about??
Compiler converts the third line i.e. Printf(a,34,a,34); to this :-





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





when the program is executed the first argument of the printf statement i.e 'main(){char *a= %c%s%c; printf(a,34,a,34); }' is executed in the following way :





main(){char *a= : gets printed as is


%c : this converts the second argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.


%s : this outputs the third argument of the Printf Statement ie. 'main(){char *a= %c%s%c; printf(a,34,a,34); }'.


%c : this converts the fourth argument of the printf ie 34 to character and prints it. The output is '"' whose ascii value is 34.


;printf(a,34,a,34); } : gets printed as is.





so the output is


main(){char *a= "main(){char *a= %c%s%c; printf(a,34,a,34); }";printf(a,34,a,34); }
Reply:this is simply illogical. I'm not sure the pointer variable *a that has char type could hold he value of a string (which is an array of char in the inside). This program is illogical, where do you get this? And what it said it do?
Reply:Well I know C++, I think they are pretty much the same.. and from what I am seeing.. uh.. this is a bit confusing.. its seeming like you are creating a pointer that is going to be pointing a place in memory that will contain character data.. then its like going to display main(){char *a= %c%s%c; printf(a,34,a,34); }34main(){char *a= %c%s%c; printf(a,34,a,34); }34 to the console screen.. Its a bit odd.. hmm, Have you tested this code out? to me don't look like it would work.. but I could be wrong..


No comments:

Post a Comment