0

I am trying to understand the basics of C programming and I'm kind of new to C, I am failing to understand why the output of my program is:

1 2
1 1

Quite alright, I understand the output from the first printf() but I do not seem to get the logic behind the second printf(). Why can't it display 1 2 as well?

#include <stdio.h>

int main()
{
    int a = 1, b = 2;

    printf("%d %d\n", a, b);
    printf("%d %d\n", a, a++);

    return 0;
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
kryticrecte
  • 377
  • 1
  • 7
  • 14

4 Answers4

5

a++ is post-incrementing a. That is, the value of a is copied before it is returned and then it is incremented.

As I mentioned in the comments, I get a different result to you, for the reason I explain below.

If you add printf("%d\n", a);, after your last call to printf() you'll see 2 because a has now been incremented.

If you want to see 1 2, you could pre-increment a (that is increment it and then use it), but you need to introduce a sequence point for this to be guaranteed to work because the order of evaluation of function arguments is unspecified by the standard and you want to use a twice:

printf("%d ", a);
printf("%d\n", ++a);

See it run!

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    No, you should use two separate `printf` statements, because the order of evaluation of function arguments is unspecified. You could just as well see `2 2`. – Chris Lutz Feb 19 '12 at 08:34
  • @ChrisLutz: I was editing my answer a you typed that. – johnsyweb Feb 19 '12 at 08:39
3

The expression a++ evaluates to the current value of a and as a side effect increments a by 1. The expression ++a evaluates to the current value of a + 1 and as a side effect increments a by 1.

If you had written

a = 1;
printf("%d\n", a++);

you would get the output 1, because you're asking for the current value of a. Had you written

a = 1;
printf("%d\n", ++a);

you would get the output 2, because you're asking for the value of a + 1.

Now, what's important to remember (especially with ++a) is that the side effect of actually updating a doesn't have to happen immediately after the expression has been evaluated; it only has to happen before the next sequence point (which, in the case of a function call, is after all of the arguments have been evaluated).

Per the language definition, an object (such as the variable a) may have its value changed by the evaluation of an expression (a++ or ++a) at most once between sequence points, and the prior value shall be read only to determine the value to be stored.

The statement

printf("%d %d\n", a, a++);

violates the second part of that restriction, so the behavior of that statement is undefined. Your output could be any of 1 1, 1 2, a suffusion of yellow, etc.

John Bode
  • 119,563
  • 19
  • 122
  • 198
2

Post Increment, increament after use

#include <stdio.h>
 int main()
    {
      in a = 1, b = 2;

      printf("%d %d\n", a, b);    // 1 1
      printf("%d %d\n", a, a++);  // 1 1
      printf("$d", a); // 2
      return 0;

      }

Following is pre-increment :

#include <stdio.h>
 int main()
    {
      in a = 1, b = 2;

      printf("%d %d\n", a, b);    // 1 1
      printf("%d %d\n", a, ++a);  // Could Be "1 1" OR "1 2"... sequence is undefined here.
      printf("$d", a); // 2
      return 0;

      }
Pheonix
  • 6,049
  • 6
  • 30
  • 48
  • 4
    To repeat what was said above: `printf("%d %d\n",a,++a);` is undefined behavior. You'll probably get "1 2" or "2 2", but anything at all could happen. – bames53 Feb 19 '12 at 08:38
0

Actually, ++x and x++ increment x the same way. The only difference is that ++x returns a reference to x and x++ returns the previous value as a temporary. The "incremented after it's been used" explanation is incorrect.

NFRCR
  • 5,304
  • 6
  • 32
  • 37
  • 1
    C (which, despite the C++ tag, is what the OP really appears to be using) doesn't have references. `++x` returns an lvalue and `x++` an rvalue, though using the lvalue from `++x` is clearly a terrible idea. – Chris Lutz Feb 19 '12 at 09:00
  • Whether a temporary is used depends on the CPU ISA. (Yes, that's how it tends to happen these days.) – Donal Fellows Feb 19 '12 at 09:00