Output of C programs | Set 65 (If-Else)
Last Updated :
13 Mar, 2023
Prerequisite : Decision making in C
Question 1
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);
}
OPTIONS:
a)Segmentation 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
C
#include"stdio.h"
int main()
{
if(-1L>1UL)
printf("paul is crazy");
else
printf("mannu is Crazy");
}
OPTIONS:
a)mannu is Crazy
b)paul is crazy
OUTPUT: (b)paul is crazy
Explanation:
Here ,comparison 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
C
#include"stdio.h"
int main()
{
int i;
if(i=0,2,3)
printf("Geeksforgeeks ");
else
printf("Hello ");
printf("%d\n",i);
}
OPTIONS:
a)Hello 3
b)Hello 0
c)Geeksforgeeks 0
d)Geeksforgeeks 3
OUTPUT: (c) Geeksforgeeks 0
Explanation: At first zero will assign in 'i' then comma operator returns the last value which is 3 and condition becomes true.
Question 4
CPP
#include"stdio.h"
int main()
{
int i;
if(i=(2,1,0))
printf("Geeksforgeeks ");
else
printf("Hello ");
printf("%d\n",i);
}
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 false.
Question 5
C
#include"stdio.h"
int main()
{
float a=0.7d;
if(a<0.7)
printf("C");
else
printf("C++");
return 0;
}
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
Similar Reads
Output of C programs | Set 43 1. What is the output of following program? C #include <stdio.h> int main() { int a = 1, b = 2, c = 3; c = a == b; printf("%d", c); return 0; } Choose the correct answer: (A) 0 (B) 1 (C) 2 (D) 3 Answer : (A) Explanation : "==" is relational operator which returns only two values, eit
3 min read
Output of C programs | Set 48 1. What is the output of following program? C #include <stdio.h> #define square(x) (x * x) int main() { int x, y = 1; x = square(y + 1); printf("%d\n", x); return 0; } Options: (A) Error (B) 4 (C) 3 (D) Garbage value Answer : (C) Explanation : The macro function square(x)(x*x) calcul
3 min read
Output of C programs | Set 30 (Switch Case) Prerequisite - Switch Case in C/C++ Interesting Problems of Switch statement in C/C++ Program 1 C #include <stdio.h> int main() { int num = 2; switch (num + 2) { case 1: printf("Case 1: "); case 2: printf("Case 2: "); case 3: printf("Case 3: "); default: printf(
2 min read
Decision Making in C (if , if..else, Nested if, if-else-if ) In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or n
7 min read
C - if Statement The if in C is the simplest decision-making statement. It consists of the test condition and a block of code that is executed if and only if the given condition is true. Otherwise, it is skipped from execution.Let's take a look at an example:C#include <stdio.h> int main() { int n = 9; // if st
4 min read
Using Range in switch Case in C You all are familiar with switch case in C, but did you know you can use a range of numbers instead of a single number or character in the case statement? Range in switch case can be useful when we want to run the same set of statements for a range of numbers so that we do not have to write cases se
2 min read