C Programming - Control Instructions - Discussion

11. 

What will be the output of the program?

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

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

Answer: Option B

Explanation:

No answer description available for this question.

Rajiya said: (Mar 15, 2011)  
In the Above program we can't declear
k=(num>(num < 10 ? 100 : 200): 500);
we given only K=(num < 10) ? 100 : 200

There in program (num < 10) is false so
it has output 200
Please Explain me sir,Thank You.

Sudhakar said: (Jul 15, 2011)  
In printf statement dey asked what is outpt of num. Not k. Here is explanation.

Here we want only output for num. So num=30.

If we want output of k then it is 200.

Venkat said: (Jul 28, 2011)  
Yeah you're exactly correct sudhakar.

Rakesh said: (Aug 13, 2011)  
Absolutely correct.

We have to read complete question, and first we have to understand logic.

Anoo said: (Aug 15, 2011)  
@sudhakar thank you.

Ktr said: (Sep 6, 2011)  
In the Above program the output is "30".
Because
-> There is no assignment statement for "num" other than "num=30"@ line 3
-> "k = (num < 10) ? 100 : 200;"
This expression will assign the value for "K" only not for num.
-> We are printing the value of "num" not "K"

Vani said: (Sep 8, 2011)  
Please explain this logic.

(num < 10) ? 100 : 200;.

Meenu said: (Sep 14, 2011)  
It means that if num is less than 10 then 100 is assigned else 200 is assigned
i.e, in ternary operator if condition is true one statement is executed else other is executed .

Syntax:

(condition)?stmt1:stmt2;

If conditon is true stmt1 is executed else stmt2.

Anurag Sharma said: (Sep 23, 2011)  
It's a simple program in which we just assign a value to k by checking conditional operator & condition comes to true.

So we have k=100 but we print num which is still 30 so 30 will print.

Prasad said: (Sep 23, 2011)  
Here num=30 is assigned the value when you compare proparly 30<10 it false No than he willbe stop and they cannot exicuted the next statment and than 30 will be print.

Varsha said: (Oct 2, 2011)  
Thanx Meenu, I was not knowing the meaning of this statement. It really helped me.

Parhiban said: (Dec 20, 2011)  
Does not use ternary operator. Because simply say that declare vale 30. Just print out the declare value.

Golu said: (Jan 23, 2012)  
Here if condition is not satisfied so it assumes the garbage value.

Manal said: (Feb 14, 2012)  
I don't understand the k=(num< 100) 100:200

Abc said: (Mar 1, 2012)  
It have the value of num not of k % its 30.

So it will print 30.

Sarath K C said: (Mar 20, 2012)  
Step 1: int k, num=30; here variable num is initialized to'30',
variable k is declared, but not initialized.
Step 2: k = (num < 10) ? 100 : 200; is nothing but
if(30 < 10)
k=100 ;
else
k=200 ;
Hence this condition will be failed. And k value
becomes 200(k=200)
Step 3: printf("%d\n", num); here it is printing num value but
not asked about to print k value.
So finally will get output 30.

Sunny said: (Aug 12, 2012)  
In the program we are printing the value of num. We are not printing the value of k. So the output is 30.

Brij said: (Dec 23, 2012)  
step 1: num = 30
step 2: k = 200
step 3: print the value of num i.e 30;

Rakesh said: (Feb 13, 2013)  
Actually we don't have any use of K there, because it is asking for num value in print out statement.

Its a type of question to know the concentration of a user.

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

Here in this program,

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

The condition is false so the result is 200 but thus in the next statement printf("%d\n", num); we print the value of num so the result is 30. what is assigned it previously?

Reddy'Siva said: (Aug 15, 2013)  
Here the question is about the value of the variable "num" and not "k" that's why the answer is 30 good thing.

Rohan Dutta said: (Sep 6, 2013)  
Here the question is about the value of "num", not "k". When we are using the condition the value of "k" has been changed. But the value of "num' remains unchanged. Therefore the output is num=30.

Jignesh Patel said: (Apr 13, 2014)  
In this programme conditional operators process does not assign value to number so output of num remains same as it is so num=30 only assignment operator can change value of number variable.

Rathan said: (Aug 19, 2014)  
In this program:

int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}

Here 30<10 condition is false.
k=200 but in printf function asked the value of 'num'.

So output is 30.

Radhanath said: (Sep 14, 2014)  
Absolutely to print the value of num=30.

Veerappa Sudhakaran said: (Jun 12, 2015)  
Exactly num value = 30.

Visva said: (Jun 24, 2015)  
Num value is declared as 30, in 2nd line they specified k not about m. So finally the num value printed as 30.

Sarath Chandra said: (Jul 16, 2015)  
k = (num < 10) ? 100 : 200;

Becomes false, since num=30 which is greater than 10.

So k=200. But its has nothing to do with value of num and remains same.

Therefore num = 30 itself.

Sushant Shankar Mahadik said: (Aug 24, 2015)  
k = (num<10) ?100:200 is become false &.

Here print k so answer will be 30.

P.Kranthi Kumar said: (Jul 4, 2016)  
k = (num<10)?100:200 is become false&

Here print the k, so the answer will become 30.

Sandy said: (Sep 8, 2016)  
Step 1: int k, num=30; // Here variable num is initialized to '30',
Variable k is declared, but not initialized.

Step 2: k = (num < 10) ? 100 : 200; // Is nothing but if(30 < 10) k = 100; else k=200;
Hence this condition will be failed. And k value becomes 200 (k = 200).

Step 3: printf("%d\n", num); // Here it is printing num value but not asked about to print k value.

So, finally will get output 30.

Yoga said: (Nov 4, 2016)  
Please explain me the answer.

Ravi said: (Dec 2, 2016)  
@Meenu.

You explained clearly I clarify my doubt with the help of your explanation, Thanks.

Saloni Kamboj said: (Feb 19, 2017)  
k = (num < 10) ? 100 : 200;

I am not getting this statement, Please explain it to me.

Kundana said: (May 19, 2017)  
1) int num=1; / initialized 'num' variable/.
2)k= (num<10) ? 100 : 200; // k= (30<10) if statement true then print. ("100") if statement is false then print ("200") //
3) But in program, they asked to print 'num' which is already initialized (int num = 30) so will be printed.

Prathamesh said: (May 26, 2017)  
Thanks for all the given explanation.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.