C Programming - Expressions - Discussion

13. 

What will be the output of the program?

#include<stdio.h>
int main()
{
    int k, num=30;
    k = (num>5 ? (num <=10 ? 100 : 200): 500);
    printf("%d\n", num);
    return 0;
}

[A]. 200
[B]. 30
[C]. 100
[D]. 500

Answer: Option B

Explanation:

Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement.
Step 3: printf("%d\n", num); It prints the value of variable num '30'
Step 3: Hence the output of the program is '30'


Satish said: (Jan 4, 2011)  
Can you elobrate your answer please.

Krishna said: (Feb 12, 2011)  
What does '?' mark implies here?

Sunil Kore said: (Apr 20, 2011)  
k = (num>5 ? (num <=10 ? 100 : 200): 500);
here (num <=10 ? 100 : 200) evaluated first o/p=200
hence
k = (num>5 ? 200: 500);
30>5
so ans will be 200

Jagan Mohan said: (May 14, 2011)  
@ sunil:

Here the question is regarding the value of 'num'.

You are correct in the sense of value 'k'.

Hope u can understand it.....

Ali Umar said: (Feb 2, 2012)  
Hi Krishna,

? means if & : means else.

There is some magic in the question. So don't calculate for k. May be any thing the value will store in k You have no need of that value. So leave that function. The value of num is already assigned in the program. So simply you have to print that value.

So x = 30 correct ans.

Ali Umar said: (Feb 2, 2012)  
Hi Sunil your logic is totally wrong.......

Follow this
This is if else statement in Conditional operator. ? means if & : means else
num = 30;
k = (num > 5 ?(if) (num <= 10 ? 100 : 200): 500);//Question

If condition will true it will take 1st following value

k = (num <=10 ? 100 : 200); // First Step

If condition will not true it will take following value

K = 500;

Final result will not come in First step. So you come in second step & in this step apply same rule with the value of num

k = (num <=10 ? 100 : 200);(Here again apply if & else condition)
.'. num = 30;

So K = 200; Ans

Hope you will try to understand.

Mad said: (Feb 29, 2012)  
Because of no change of k in the code the logic worked and it remain as k=30.

Thirupathi said: (Mar 10, 2012)  
Can you explain in easy manner?

Amit said: (Jun 30, 2012)  
@Thirupathi.
Skip this statement k = (num>5 ? (num <=10 ? 100 : 200) : 500) ;
Because we have to print the value of num not the value of k.
So it will print num=30.
Thanks.

Bbbb said: (Aug 17, 2012)  
i=4;
printf("i=%d,%d,%d,%d",i,++i,i++,--i);
the answer in fedora(linux platform) is 5,5,3,5...hw is it??

Ragni said: (Sep 1, 2013)  
k = ( num>5 ? (num <=10 ? 100 : 200) : 500);

Will above statement show error message or not?

Luis said: (Jun 25, 2014)  
Ternary operators work like this:

int a,b,c;

a = (condition) ? value if true : value if false;

In this example there is a nested ternary operation, but the question was tricky.

Krishhnakuamr said: (Sep 20, 2014)  
Explain if we print k value what will happen in the k = (num>5 ? (num <=10 ? 100 : 200): 500);

Teja said: (Jan 30, 2015)  
#include <stdio.h>

int main() {
int a=400,b=700;
if(!a<=500)
{

b=80;
c=90;

printf("%d %d",b,c);
}
return 0;
}

How does the condition works with a works? Can u please help me?

Tamilmaran C said: (Jun 24, 2015)  
Dear Sir,

According to your problem resolve, It could not be:

k = (num>5 ? (num <=10 ? 100:200):500).

Given.

k = (num>5 ? (num <=10 ? 100:200):500);

Answer : 30.

Actual answer : 30 (but expression error available).

Tamilmaran C said: (Jun 24, 2015)  
Its independent to Ternary operator because software expression are differ with this.

Purni said: (Mar 4, 2016)  
Could you explain what o/p will be the result of

int x=3, y=4, z=4;

printf("ans=%d",(z>=y>=x?100:200));

Emigo said: (Mar 6, 2016)  
@Purni.

z >= condition?if condition is true pick 100: else 200.

Now, putting values.
z >= 4 >= 4?100:200 ///condition is true.
z >= 100.,
4 >= 100. // its false.

Ans is 0.

Ranjithkumar S said: (Oct 7, 2016)  
Look at the printf statement clearly. They asking the value of num not the value of k.

Sweety said: (Aug 4, 2017)  
I think 200 is the correct answer.

Karthik said: (Dec 24, 2017)  
@ALL.

Look at this carefully. They asked only the value of num not the value of k.

Nagaraj said: (Aug 22, 2018)  
We are printing only num value i.e. num = 30 in the program, not k value!. Don't consider about expression. It may be true or false.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.