C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 13)
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;
}
Answer: Option
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'
Discussion:
27 comments Page 2 of 3.
Karthik said:
8 years ago
@ALL.
Look at this carefully. They asked only the value of num not the value of k.
Look at this carefully. They asked only the value of num not the value of k.
Ranjithkumar S said:
9 years ago
Look at the printf statement clearly. They asking the value of num not the value of k.
Emigo said:
9 years ago
@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.
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.
Purni said:
9 years ago
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));
int x=3, y=4, z=4;
printf("ans=%d",(z>=y>=x?100:200));
Tamilmaran c said:
1 decade ago
Its independent to Ternary operator because software expression are differ with this.
Tamilmaran C said:
1 decade ago
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).
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).
Satish said:
1 decade ago
Can you elobrate your answer please.
Krishhnakuamr said:
1 decade ago
Explain if we print k value what will happen in the k = (num>5 ? (num <=10 ? 100 : 200): 500);
Ragni said:
1 decade ago
k = ( num>5 ? (num <=10 ? 100 : 200) : 500);
Will above statement show error message or not?
Will above statement show error message or not?
Bbbb said:
1 decade ago
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??
printf("i=%d,%d,%d,%d",i,++i,i++,--i);
the answer in fedora(linux platform) is 5,5,3,5...hw is it??
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers