Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : if statement - 1Next Topic : if else - 1

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “if statement”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “if statement” along with answers and/or explanations.

Q1: What will be the output of this c code?
#include<stdio.h>
int main()
{	int a=0,b=1,c=0;
		if(++a, b, c++)
			printf("%d %d %d",a,b,c);
	return 0;
}
a)  1 1 1
b)  1 1 0
c)  0 1 0
d)  Nothing
Correct Answer : d
Explanation : Nothing will be printed, because the value of c++ is used inside if as a test condition , and that is 0, so the printf inside if will not be executed.


Q2: What will be the output of following c code?
#include<stdio.h>
int main()
{
    int i=1;
    if(i=2);
    printf("%d",i);
    return 0;
}
a)  1
b)  2
c)  Syntax Error
d)  Runtime Error
Correct Answer : 2
Explanation : in if the value of i is updated to 2, then printf() prints the value of i so the value of i is printed 2. Note: using a ; after if denotes that the statement after if are not controlled by this if statement.


Previous Topic : if statement - 1Next Topic : if else - 1