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;
}
Discussion:
39 comments Page 1 of 4.
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.
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.
Sandy said:
9 years 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.
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.
(1)
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?
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?
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"
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"
Kundana said:
8 years ago
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.
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.
(3)
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.
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.
Rathan said:
1 decade ago
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.
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.
(1)
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.
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.
Rohan Dutta said:
1 decade ago
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.
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.
So we have k=100 but we print num which is still 30 so 30 will print.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers