Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : Increment Decrement Operator - 1Next Topic : Conditional Operator - 1

This section on C interview questions, tricky questions, and puzzles focuses on “Increment Decrement operator”. 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. Here is a listing of C questions and puzzles on “Increment Decrement operators” along with answers and/or explanations.

Q1: What will be the output of the following C code?

#include <stdio.h>
void main()
{
  int x = 97;
  int y = sizeof(x++);
  printf("X is %d", x);
}
a)  X is 98
b)  X is 97
c)  Syntax Error
d)  Runtime Error
Correct Answer : b
Explanation : Increment Decrement operator does not execute in side the parenthesis of sizeof operator.


Q2: What will be the output of following  c code?
#include <stdio.h>   
int main() 
{  int a = 1,b = 1,c,d; 
   c = a || --b; 
   d = a-- && --b; 
   printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d); 
   return 0; 
} 
a)  a = 1, b = 1, c = 1, d = 1
b)  a = 0, b = 0, c = 0, d = 0
c)  a = 0, b = 1, c = 1, d = 0
d)  a = 0, b = 0, c = 1, d = 0
Correct Answer : d
Explanation : Initial value of a and b is 1. After expression c=a||--b; c holds 1, a holds 1 and b holds 1, because --b is not executed due to short circuit in logical or operators.
After expression a-- && --b both a and b becomes 0, and d also holds 0.
Learn About ++ and --


Q3: What will be the output of following program ?
#include <stdio.h>
int main()
{	int x;
	x=- -20;
	printf("x=%d,",x);
	x=+ +20;
	printf("x=%d",x);
	return 0;
}
a)  Syntax Error
b)  x=19,x=21
c)  x=20,x=20
d)  Runtime Error
Correct Answer : c
Explanation : Here unary minus (or negation) operator is used twice. So - -20 results in 20, similarly + +20 results in 20.


Previous Topic : Increment Decrement Operator - 1Next Topic : Conditional Operator - 1