C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}";

int main()
{
    printf(str, 34, str, 34);
    return 0;
}
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}
char *str = %c%s%c; main(){ printf(str, 34, str, 34);}
No output
Error in program
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 1 of 3.

Sharath said:   1 decade ago
Can anyone explain this please?

Snehal said:   1 decade ago
#include<stdio.h>
void main()
{
char ch=291;
printf("%d %d %d",32770,ch,ch);
}

Kavyashree said:   1 decade ago
Its because printf(str , 34 , str , 34) is replaced as

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

So it will print untill it finds first ending double quot. So rest of the things will be ignored.

Sankar said:   1 decade ago
Nice program.

Gshankar said:   1 decade ago
I can't understand could anyone explain this properly.

Sundar said:   1 decade ago
I got the following output in 32 bit GCC compiler.

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

Pavitra said:   1 decade ago
I can't undeastand please explain it.

Vijji said:   1 decade ago
I want detailed explanation. Please explain this bit.

Suyash said:   1 decade ago
printf(str, 34, str, 34);

expands to:

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

then,

1.printf prints: char *str =
2.%c is nothing but ASCII value for double quote ie (")
3.%s prints: char *str = %c%s%c; main(){ printf(str, 34, str, 34);}
4.%c prints: (")
5.prints: ; main(){ printf(str, 34, str, 34);} [It is the remaining portion of expanded str}.
(1)

Ashok Phour said:   1 decade ago
printf(str,34,str,34);

1. On the first parameter (str) it place it.
"char *str = %c%s%c; main(){ printf(str, 34, str, 34);}";

2. Here 3 format specifier %c%s%c (char,string,char) so on the first specifier it place (") (ASCII value of 34=") ,
then
On the second time print full string (str) to (%s) specifier.

On the last format specifier (%c) it will place (") because last variable is (34=").


Post your comments here:

Your comments will be displayed after verification.