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 4 of 4.

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

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

VANI said:   1 decade ago
Please explain this logic.

(num < 10) ? 100 : 200;.

KTR said:   1 decade ago
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"

Anoo said:   1 decade ago
@sudhakar thank you.

Rakesh said:   1 decade ago
Absolutely correct.

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

Venkat said:   1 decade ago
Yeah you're exactly correct sudhakar.

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

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


Post your comments here:

Your comments will be displayed after verification.