Output of C programs | Set 65 (If-Else)

Prerequisite : Decision making in C

Question 1
[sourcecode language=”C”]
#include"stdio.h"
#include"stdlib.h"
void reverse(int i)
{
if (i > 5)
exit(0);
printf("%d\n", i);
return reverse(i++);
}
int main()
{
reverse(1);
}
[/sourcecode]
OPTIONS:
a)Segmenation fault
b)Compilation error
c)Print 1 Infinite time
d)Both a & c

OUTPUT:  (d)Both a & c

Explanation:
We call the main method again and again by 1 because we use post-increment. At certain time stack frame will full means segmentation fault occurs.

Question 2
[sourcecode language=”C”]
#include"stdio.h"
int main()
{
if(-1L>1UL)
printf("paul is crazy");
else
printf("mannu is Crazy");
}
[/sourcecode]
OPTIONS:
a)mannu is Crazy
b)paul is crazy

OUTPUT:  (b)paul is crazy 

Explanation:
Here ,comparision between long int and unsigned long int generally which is not possible.
Now,long int is promoted to unsigned long int whose value will be
(2^size_of_unsigned_long_int)-1.

Question 3
[sourcecode language=”C”]
#include"stdio.h"
int main()
{
int i;
if(i=0,2,3)
printf("Geeksforgeeks ");
else
printf("Hello ");
printf("%d\n",i);
}
[/sourcecode]
OPTIONS:
a)Hello 3
b)Hello 0
c)Geeksforgeeks 0
d)Geeksforgeeks 3

OUTPUT:  (d) Geeksforgeeks 0

Explanation: Atfirst zero will assign in ‘i’ then comma operator returns the last value which is 3 and condition becomes true.

Question 4
[sourcecode language=”CPP”]
#include"stdio.h"
int main()
{
int i;
if(i=(2,1,0))
printf("Geeksforgeeks ");
else
printf("Hello ");
printf("%d\n",i);
}
[/sourcecode]
OPTIONS:
a)Hello 3
b)Geeksforgeeks 0
c)Hello 0
d)Geeksforgeeks 3

OUTPUT:  (c) Hello 0

Explanation: Priority of parenthesis bracket is greater than equal to(=) operator , So atfirst comma operator return the last value which is zero(0) and then equal to(=) operator assign 0 to ‘i’ and condition becomes flase.

Question 5
[sourcecode language=”C”]
#include"stdio.h"
int main()
{
float a=0.7d;
if(a<0.7)
printf("C");
else
printf("C++");
return 0;
}
[/sourcecode]
OPTIONS:
a)Compilation error
b)C++
c)C

OUTPUT:  (C)C 

Explanation:
a = 0.7 is rounded to 0.699999988
and the constant 0.7 is as 0.69999999999
so a<0.7 is true so it print "c"
but in case of 0.8
a = 0.800000011 and
constant 0.8 is 0.8000000000000000

This article is contributed by Abhishek kurmi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.