0

I don't understand why a equals 1 and b equals 0 at the end. They should be the same in my view. Thanks in advance.

#include "stdio.h"

int main()
{
    int a=0;
    int b=0;
    a++;
    printf("a=%d,b=%d",a,b++);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Jaden
  • 151
  • 6

3 Answers3

5

Before this call:

printf("a=%d,b=%d",a,b++);

the variable a was already incremented by:

a++;

However in the pointed-to call of printf, the value of the post-increment expression b++ is the value of the variable b before its increment. So 1 and 0 are output.

If you want to get the output 1 and 1, then use the pre-increment expression with the variable b like:

printf("a=%d,b=%d",a,++b);

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)...

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

The operation b++ sends b to printf before doing the increment. a is incremented before printf is called

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Actually, the increment must be completed before `printf` is called, because C 2018 6.5.2.2 10 says there is a sequence point after the evaluations of the argument and before the call. So `b` is not “sent” to `printf` before the increment. Rather, the **value** of `b++` is the value of `b` before the increment, and it is this value, not `b`, that is passed to `printf`. – Eric Postpischil Dec 09 '19 at 21:09
  • I was trying to keep it simple – Ed Heal Dec 09 '19 at 21:22
  • Simply, the value of `b++` is the value of `b` before the increment, and `b` is incremented as a side effect. – Eric Postpischil Dec 09 '19 at 21:37
2
printf("a=%d,b=%d",a,b++);

Is logically equivalent to:

printf("a=%d,b=%d",a,b);
b++; 
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
  • These are not equivalent; the increment to `b` must be performed before the function call occurs, per C 2018 6.5.2.2 10. If `b` were accessible to the called function (because it is defined with external linkage or its address has been made available in some way), the called function would see the incremented value of `b` with the former but not with the latter. – Eric Postpischil Dec 09 '19 at 21:11
  • @EricPostpischil I said "logically" equivalent. – Fiddling Bits Dec 09 '19 at 21:13
  • They are not logically equivalent; they have different observable behaviors. – Eric Postpischil Dec 09 '19 at 21:15
  • @EricPostpischil I'll have to look into it. – Fiddling Bits Dec 09 '19 at 21:15
  • After `#include `, `int b=0; void foo(int x) { printf("%d %d\n", x, b); } int main(void) { foo(b++); }` prints “0 1”, but `int b=0; void foo(int x) { printf("%d %d\n", x, b); } int main(void) { foo(b); b++; }` prints “0 0”. – Eric Postpischil Dec 09 '19 at 21:18
  • @EricPostpischil Thanks. – Fiddling Bits Dec 09 '19 at 21:19