C Interview Questions on IF Else Conditions

We discuss various C Language interview questions about If else conditions.

Interview Questions

  1. What represents true or false in a condition?
  2. What is the output of the below if condition?
  3. What is the output of the below if condition?
  4. What is the output of the below if condition?
  5. What is the output of below if program example?
  6. What is the output of below if example program?

What represents true or false in a condition?

Non – Zero Value : True

Zero Value : False

Examples below illustrates the concept further.

What is the output of the below if condition?

#include <stdio.h>
#include <stdlib.h>

int main() {
	if(1)
	{
		printf("if(1) is true");//if(1) is true
	}
}

printf in the if condition is executed. 1 is non zero. So, it is true.

What is the output of the below if condition?

#include <stdio.h>
#include <stdlib.h>

int main() {
	if(-1)
	{
		printf("if(-1) is true");//if(-1) is true
	}
}

printf in the if condition is executed. -1 is also non zero. So, it is true. Even negative numbers represent a true condition in an if.

What is the output of the below if condition?

#include <stdio.h>
#include <stdlib.h>

int main() {
	if(0)
	{
		printf("if(0) is true");//DOES NOT PRINT ANYTHING
	}
}

printf in the if condition is NOT executed. 0 represents false. So, the printf is not executed.

What is the output of below if program example?

#include <stdio.h>
#include <stdlib.h>

int main() {
	int i = 15;

	if(i==3)
		printf("First Statement");
		printf("Second Statement");
}

Only the second if statement is executed. Only one line is part of the if. So, the program above is similar to the program below. It is recommended to use braces in if statement even if there is only statement in the if. It makes the if statement easier to read and understand.

#include <stdio.h>
#include <stdlib.h>

int main() {
	int i = 15;

	if(i==3){
		printf("First Statement");
	}
	
	printf("Second Statement");
}

What is the output of below if example program?

#include <stdio.h>
#include <stdlib.h>

int main() {
	int i=4,j=7;

	if(i==3)//FIRST IF
		if (j==6)//SECOND IF
			printf("i==3 j==6");
	else
		printf("i!=3");

}

No Output. Nothing is printed to the output. The formatting of above program is not the same as the execution. The else part belongs to the second if. The correct formatting of the program is as below.

#include <stdio.h>
#include <stdlib.h>

int main() {
	int i = 4, j = 7;

	if (i == 3) //FIRST IF
		if (j == 6) //SECOND IF
			printf("i==3 j==6");
		else
			printf("i!=3");

}
FREE JAVA INTERVIEW GUIDE

ONE SUBSCRIBER EVERY MONTH WINS 100% DISCOUNT CODE FOR INTERVIEW GUIDE WITH 23 VIDEOS

We Respect Your Privacy