Increment-Decrement C Interview Question

2.2K views 2 minutes read
A+A-
Reset

This question is very strong way to evaluate your idea about the increment/decrement unary operator in C. Being the most efficient way of increasing or decreasing a value, these operators are abandon in almost every piece of code. This question will enhance your concept about the Increment and Decrement operator in huge extent. But keep in mind that This question is not related to postfix and prefix types of these operator because these are extensively covered in almost all university courses.

Question:

What is the output of following code snippet?

void main(){
int a=3,b=5;
printf("%d",++(a+b-1));
getch();
}

Answer:

I’m sure that you are aware of the inx/dcx unary operators. At a glance it may seem like here is a prefix increment operator in above expression. But if you explore it more precisely you will find something wrong.

Lets explore some facts about Increment operator.

  1. Increment or decrements operator can be only applied with variables so We can not use it with values, literals or expressions.
    For example: a=3++ ; is totally invalid and will lead to compile time error.
  2. Another point is nesting of increment or decrement operator is not valid at all. That means ++(a++) is invalid expression.
  3. Also last thing it can be applied with float or double variables.

Now here In our code It violate the first fact mentioned above.Increment Operator

Therefor the above code lead to compile time error saying :  error: lvalue required as increment operand

So here You exploit something about inx/dcx unary operator and hope you can give shut it down response in this kind of question.

Previously I have some Interview Questions about Size of Pointer. That may be very useful content if you are preparing for job or enhance your knowledge for any matter.

If you have any suggestion please drop a mail or feel free to comment.

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.