I don't know why the code in c language came up with this value

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

//#include <stdio.h>

int main(void) {
  int num = 5;

  num &= ++num;
  printf("%d", num);

  num &= num++;
  printf("%d", num);

  return 0;
}

Number 1

Number 2

"Connect because there is no \n"

Answer: 45

I need to get one.

The answer is 66...

What's the problem?

c

2022-09-20 10:21

1 Answers

num &= ++num;

One line above is the same as the two lines below are executed in order.

++num;
num&=num;

Meanwhile,

num &= num++;

One line above is the same as the two lines below are executed in order.

num&=num;
num++;

The question said 66 would be printed, but if you actually turn it around, 67 will be printed. For this reason, check the description above and the comments in the code below.

#include <stdio.h>

int main(void) {
  int num = 5;

  num &= ++num;
// First, num is increased by 1, and num becomes 6
// After that, the result of num &= num; is num=6&6, so num is eventually 6.

  printf("%d", num);

  num &= num++;
// The current num is 6, and the result of num & = num; is num=6&6, so num is still 6.
// After that, num++ runs and num becomes 7.

  printf("%d", num);

  return 0;
}


2022-09-20 10:21

If you have any answers or tips


© 2022 pinfo. All rights reserved.