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 2 of 3.

Vishalakshi said:   9 years ago
Thanks @Vishnupriya.

Adi said:   9 years ago
Thank you @Vishnupriya.

Vishnupriya said:   9 years ago
@Nivethitha

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

Given print stmt: printf(str, 34, str, 34);

Here,

The first str in printf is replaced by "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}";

And the remaining arguments are as such, thus it will now become

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

The first character is " so the contents will be starting printing to output..while printing
the escape sequence %c is replaced by ASCII value of 34 which is "

Then %s is replaced by str which is "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"--the escape sequence here are not substituted bcoz we have not specified any variables for it..

%c is again replaced by ASCII value of 34 which is "

Thus output is:
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}
(1)

Nivethitha said:   9 years ago
Why once again main() printf(str,34,str,34) is printed at last? I don't understand. Please can anyone explain this code?

Siva sangeetha said:   1 decade ago
How could understand in easiest way?

"=asci value of 34.

Then str is? How and what will be correct?

Siva said:   1 decade ago
What is mean by char*string;? Could you explain please?

Anubhav singh (Funky) said:   1 decade ago
OK ! I can explain it in a easier way:

Consider the statement:-

printf("abc1 %c %s %c abc2", 10, xyz, 10);

You can see its a simple printf statement.

Now replace, abc1 => char *str =

abc2 =>; main(){printf(str, 34, str, 34);}

10=> 34.

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

You will get the answer.

Lakshmeesh said:   1 decade ago
Still I am facing problem in input and output statements.

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=").

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)


Post your comments here:

Your comments will be displayed after verification.