C Programming - Input / Output - Discussion

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;
}

[A]. char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}
[B]. char *str = %c%s%c; main(){ printf(str, 34, str, 34);}
[C]. No output
[D]. Error in program

Answer: Option A

Explanation:

No answer description available for this question.

Sharath said: (Sep 25, 2010)  
Can anyone explain this please?

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

Kavyashree said: (Oct 13, 2010)  
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: (Jan 19, 2011)  
Nice program.

Gshankar said: (Sep 15, 2011)  
I can't understand could anyone explain this properly.

Sundar said: (Oct 25, 2011)  
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: (Nov 6, 2011)  
I can't undeastand please explain it.

Vijji said: (Nov 12, 2011)  
I want detailed explanation. Please explain this bit.

Suyash said: (Mar 7, 2012)  
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}.

Ashok Phour said: (May 11, 2012)  
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=").

Lakshmeesh said: (Aug 6, 2014)  
Still I am facing problem in input and output statements.

Anubhav Singh (Funky) said: (Jun 22, 2015)  
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.

Siva said: (Jul 24, 2015)  
What is mean by char*string;? Could you explain please?

Siva Sangeetha said: (Jul 24, 2015)  
How could understand in easiest way?

"=asci value of 34.

Then str is? How and what will be correct?

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

Vishnupriya said: (Jun 20, 2016)  
@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);}

Adi said: (Nov 5, 2016)  
Thank you @Vishnupriya.

Vishalakshi said: (Dec 3, 2016)  
Thanks @Vishnupriya.

Amrit said: (Mar 27, 2017)  
I'm not getting this.

Please give a proper explanation.

Mass said: (Jul 12, 2017)  
Awesome explanation, thank you all.

Harish said: (Jul 12, 2017)  
Thanks @Kavyashree.

Shekhar Balaskar said: (Dec 3, 2017)  
@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);}.

Pooja said: (Jun 19, 2018)  
Good explanation thanks @Vinshnupriya.

Krishu said: (Jun 20, 2018)  
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);}.

Anonymous said: (Jul 22, 2018)  
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?

Saurabh said: (Dec 11, 2018)  
Very helpful. Thanks for the answer @Vishnupriya.

Anne said: (Sep 25, 2020)  
Thank you @Krishu.

Reddy said: (Mar 21, 2021)  
What is it? Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.