C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 2)
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;
}
C-program
Ps
Error
None of above
Answer: Option
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".

Discussion:
9 comments Page 1 of 1.

Biswabhusan Dixit said:   6 years ago
@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.

Shubham said:   9 years ago
Conditonal statement is used in printf func nothing logic is used.

Yashwanth said:   10 years ago
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.

Y saikrishna said:   1 decade ago
I didn't understand the above can anyone help me in brief.

Priyatharshini said:   1 decade ago
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.

Sravanthi reddy said:   1 decade ago
@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.

Biswajit said:   1 decade ago
"ps" is nothing but it will print if the condition (a>10).

Rani said:   1 decade ago
What about "Ps"?

K.suriya said:   1 decade ago
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.

Post your comments here:

Your comments will be displayed after verification.