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.

Reddy said:   4 years ago
What is it? Please explain.

Anne said:   5 years ago
Thank you @Krishu.
(1)

Saurabh said:   6 years ago
Very helpful. Thanks for the answer @Vishnupriya.

Anonymous said:   7 years ago
Why it prints from char * str = "char *str str.." and not from the quotes.? The string variable str refers to the strings within the double quotes?

I actually don't understand this. How is this possible?

Krishu said:   7 years ago
I didn't get anybody's logic and neither found anybody's logic matchning the answer. So, what i interpret from this main statement is :

#include<stdio.h>
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; --> (this is a string definition global to program)

int main()
{
printf(str, 34, str, 34); --> ( this is a printf statement which outputs the string )
return 0;
}

ELABORATING --> printf(str, 34, str, 34);

1st step : ( printf(str
SO, this goes to the string str and prints it
WE GET : ---> char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; <--- IN OUTPUT

2nd step : (printf(str,34
SO, this says or this moves the string to point to 34 position ( including the count of spaces as this is a string)
NOW, string points to m of main

3rd step : (printf(str,34,str
NOW, this will print the string from m character of the main()
i.e will output ---> main(){ printf(str, 34, str, 34);}";

step 4 : (printf(str,34,str,34)
THIS will increment the string again by 34.

SO, FINAL OUTPUT FROM 1st step and 3rd step is;
COMBINING, WE GET FINAL OUTPUT AS,
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}.
(8)

Pooja said:   7 years ago
Good explanation thanks @Vinshnupriya.

Shekhar balaskar said:   7 years ago
@ALL.

In this program, first it takes pointer to the first chart in *str(that is starting address of str),
In printf statement first it printf the portion till the ""(double couts) that is ASCI value of 34, %c prints the ASCII value of 34, %s prints str value after that it print "(closing couts),after that it again prints the main function (that is remaind):
printf(str)-char *str =||.

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

HARISH said:   8 years ago
Thanks @Kavyashree.

MASS said:   8 years ago
Awesome explanation, thank you all.

Amrit said:   8 years ago
I'm not getting this.

Please give a proper explanation.


Post your comments here:

Your comments will be displayed after verification.