2

I don't understand how the results of this is :

2
2
2

Here is my code :

#include <stdio.h>
int main()
{
int a = 1, b = 1, x = 0, y = 0;
double w;
x = 1 + a++;
printf("x = %d\n", x);
printf("a = %d\n", a);
y = ++b;
printf("y = %d\n", y);
printf("b = %d\n", b);
}

Ok i understood the postfix and prefix but i still don't understand why a and b are 2 and not 1 . They are not being saved anywhere so when you say x=1+a++ and y=++b, b becomes 2 and is saved in y . How does b keeps being 2 when not saved anywhere such as b=++b .

Sorry i am not sure if you guys follow what i am thinking.

2 Answers2

3

You have to understand how the increment operator works.

You have two operations :

  • X++ => Return the value of X, then increment it by 1.
  • ++X => Increment X by 1 then return it.

In your situation, the line with a problem is here : x = 1 + a++;

This translates into :

  • Return the value of a (1) and increment it (a becomes 2).

  • Set the value of x equal to the 1 + the value returned by a (1) (x becomes 2)

Hope this helps.

Corb3nik
  • 1,177
  • 7
  • 26
  • Ok i understood the postfix and prefix but i still don't understand why a and b are 2 and not 1 . They are not being saved anywhere so when you say x=1+a++ and y=++b, b becomes 2 and is saved in y . How does b keeps being 2 when not saved anywhere such as b=++b . Sorry i am not sure if you guys follow what i am thinking. – eternity123 Oct 04 '15 at 21:30
  • 3
    @eternity123 Using ++b is equivalent of saying `b = b + 1`. In other words, doing ++ DOES save in that variable. – Corb3nik Oct 04 '15 at 21:33
  • 1
    Oh that clears a lot thanks .Thanks for everyone who answered my questions in god speed. I really like this website. Take care everyone. – eternity123 Oct 04 '15 at 21:34
  • @eternity123 No problem, feel free to set my answer as official if this has helped you. – Corb3nik Oct 04 '15 at 21:35
  • i would but i have no clue how haha – eternity123 Oct 04 '15 at 21:38
  • 1
    @eternity123 Click on the check-mark to the left of my answer. – Corb3nik Oct 04 '15 at 21:50
1

C Language Pre-increment and Post-increment Operators

#include <stdio.h>
int main()
{
    int a = 1, b = 1, x = 0, y = 0;
    double w;
    x = 1 + a++;
    printf("x = %d\n", x);
    printf("a = %d\n", a);
    y = ++b;
    printf("y = %d\n", y);
    printf("b = %d\n", b);
}

Pre-increment means increment variable before its value is used. Post-increment means increment variable after its value has been used. To understand how these operators work, let's look at the first case:

a = 1;
x = 1 + a++; // ++ on right means post-increment

First you're setting the value of a to 1. Then you're saying add a (value is 1) to x (value is 1). The result is x has a value of 2. And then, after the statement is done executing, as a side-effect, a is incremented, because you used the post-increment form of ++. So after x has been set to a value of 2, the value of a will become 2.

Instead, if you used a a pre-increment operator:

a = 1;
x = 1 + (++a); // ++ on left means pre-increment

Again, you start with a = 1, but this time, a is incremented before its value is used in the statement, because ++ on the left-side means pre-increment. In other words, first, a is incremented to the value of 2. Then a is added to x (whose value is 1), setting the value of x to 3.

In both the above cases, a starts out with a value of 1 and becomes 2. The difference is whether that happens before or after the value of a is used in the expression.

clearlight
  • 12,255
  • 11
  • 57
  • 75
  • The side-effect could occur any time during the execution of the statement, in both cases. There is no before/after ordering between the evaluation and the side-effect. – M.M Oct 04 '15 at 23:58