-2

Can anybody tell me how the answer of this code is 16 25

#include<stdio.h>
#define sqr(x) ++x * ++x
int main()
{
  int a = 3, z;
  z = ++a * ++a;     
  a -= 3;
  printf("%d %d", sqr(a), z);
  return 0;
}  
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

Compiling the code with -Wall option gives the following warning:

[Warning] operation on 'a' may be undefined [-Wsequence-point]

The results are platform and version dependent. Using such an expression results undefined behaviour. Therefore, such an expression must not be used.

Burak
  • 2,251
  • 1
  • 16
  • 33
  • So if two or more pre/post increment or decrement operators are multiplied (or divided or added or subtracted),then we should first evaluate increment or decrement operator then perform multiplication or division? – Kris Mar 27 '20 at 17:35
  • 1
    Not only it's not recommended, it's **plain wrong** to do this. 1: it's undefined behaviour, 2: the code is hard to understand. – Jabberwocky Mar 27 '20 at 17:48
  • Actually it's not mine code.This code was a MCQ asked in the exam. – Kris Mar 27 '20 at 21:35