C Programming - Control Instructions - Discussion

2. 

What will be the output of the program?

#include<stdio.h>
int main()
{
    char str[]="C-program";
    int a = 5;
    printf(a >10?"Ps\n":"%s\n", str);
    return 0;
}

[A]. C-program
[B]. Ps
[C]. Error
[D]. None of above

Answer: Option A

Explanation:

Step 1: char str[]="C-program"; here variable str contains "C-program".
Step 2: int a = 5; here variable a contains "5".
Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as


if(a > 10)
{
    printf("Ps\n");
}
else
{
    printf("%s\n", str);
}

Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str.

Hence the output is "C-program".


K.Suriya said: (May 14, 2011)  
Printf statenment contains one if condition with true block and else block a>10 means if (a>10) the condition is false so the else statement will execute print the str value is c_program.

Rani said: (Feb 15, 2012)  
What about "Ps"?

Biswajit said: (Mar 29, 2012)  
"ps" is nothing but it will print if the condition (a>10).

Sravanthi Reddy said: (Aug 29, 2014)  
@Rani.

We get ps as a output only when the condition a>10 is satisfies but acc to the given data the condition is failed so pn is not an output.

Priyatharshini said: (Oct 16, 2014)  
This is an ternary or conditional operator that's why it execute exp2 in this operator.

If the given condition is true exp1 will execute else exp 2 will execute here the condition is (a>10) but the value of a is 5.

So the given condition is false that's the answer will be printed as c-language.

Y Saikrishna said: (Nov 6, 2014)  
I didn't understand the above can anyone help me in brief.

Yashwanth said: (Feb 5, 2016)  
Sorry but I did not get any of above explanation because as there is no value assigned to ps how it comes to role here.

Shubham said: (Nov 13, 2016)  
Conditonal statement is used in printf func nothing logic is used.

Biswabhusan Dixit said: (Jun 30, 2019)  
@Saikrishna.

Here is the use of conditionl operator .The syntax for conditional operator is :
var=(condition)?statement1:statement2;

If the condition will be true then the 1st statement is executed and for false statement2.
so, here condition false so the 2nd statement will be that means pintf("%s",str); will be remaining for str print c-programming.

That's all thanks.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.