C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 11)
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;
}
200
30
100
500
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
39 comments Page 2 of 4.

Varsha said:   1 decade ago
Thanx Meenu, I was not knowing the meaning of this statement. It really helped me.

Parhiban said:   1 decade ago
Does not use ternary operator. Because simply say that declare vale 30. Just print out the declare value.

Golu said:   1 decade ago
Here if condition is not satisfied so it assumes the garbage value.

Manal said:   1 decade ago
I don't understand the k=(num< 100) 100:200

Abc said:   1 decade ago
It have the value of num not of k % its 30.

So it will print 30.

Sarath K C said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
step 1: num = 30
step 2: k = 200
step 3: print the value of num i.e 30;

Rakesh said:   1 decade ago
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:   1 decade ago
#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?


Post your comments here:

Your comments will be displayed after verification.