1

I am writing a program in C++. I am having a problem with the code when I assign multiple values to a single variable. I am confused about the particular logic as there is no decrement operator in the code but when i assign the multiple values its logic is not completely understood by me:

int main()
{
    int j = 1;
    int i = (j+2, j+3, j++);
    cout<<"value is "<<i;
    getch();
    return 0;
}

Output is 1. I don't know one is assigned to i.

2
  • 3
    That's not assigning multiple values to a single variable, it's not possible. You need to read about the comma operator. Commented Dec 14, 2013 at 13:16
  • The comment by Joachim says it all. Commented Dec 14, 2013 at 13:28

5 Answers 5

2

When you write int i=(j+2,j+3,j++);, it's basically like you wrote int i=j++;.

That means:

1) since the value of j is 1, i will have the same value;

2) after that line j will be incremented.

Sign up to request clarification or add additional context in comments.

Comments

1

As others have pointed out, you cannot do what you want to do, and the comma operator is coming into play. To be crystal clear, this line:

int i=(j+2,j+3,j++);

Is performing these steps:

  1. j+2 is evaluated. This evaluates to 3, which is discarded as it is not assigned to anything.
  2. j+3 is evaluated, see step 1.
  3. j++ is evaluated. This increments j, whose value becomes 2, but evaluates to the OLD value of j, which is 1.
  4. The result of step 3 is assigned to i.

Comments

0

variable j is post increment so the current value of j is put into i and then j is incremented.

You can think of this as being:

int j=1;
int temp = j + 2;
int temp1 = j + 3;
int i=j;
j++;

Where the variables temp and temp1 do not really exist and are thrown away.

2 Comments

int i=(j++,j+2,j+3); i got your point but why the answer will become 5 instead of 4 in this case.
Because j=1; then j++; then i = j+3; so j=1 then j = 2 then i = 2 + 3;
0

Let consider these definitions

int j=1;
int i=(j+2,j+3,j++);

(j+2,j+3,j++) is an expression with the comma operator. Its result is the last subexpression. Subexpressions j + 2 and j + 3 do not influence neither j nor i. The only subexpression that influence j and i is the last. It is j++. The value of postincrement operator is the value of the original variable before the increment. So the value of j++ is 1. And i will get this value. At the same time j will be incressed and get value 2.

Comments

0

Read: Comma Operator: ,

The comma operator , has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

The expression:

i = (j + 2, j + 3, j++);

is in effects equivalent to (because evaluation of j + 2 and j + 3 has no side-effects):

i = j++;

So first value of j is assigned to i that is 1 then j incremented to 2.
Here j + 2 and j + 3 are evaluated before j++ but it has no effect.

Important to note parenthesis ( ) in expression over write the precedence so first , operator evaluated within parenthesis and then = evaluates at last.

To understand the output: look at precedence table , have lower precedence than =. In you expression you have overwrite the precedence using parenthesis.

Edit: on the basis of comments:

Suppose if expression is:

int i = (j++, j+2, j+3);

In this expression j++ first increase value of j because of ++ operation to 2 then j + 2 evaluates but this sub-expression has no side effects finally j + 3 evaluates = 5 and 5 is assigned to i. so value to i at the end is 5.

Check working code

So here int i =(j++, j+2, j+3); is NOT equivalent to just i = j + 3 because j++ has side effect that change value of j.

In sort if you have an expression like a = (b, c, d) then first expression b evaluates then expression c evaluates then expression d evaluates due to left-to-write associativity of , operator then final value of expression d is assigned to variable a.

5 Comments

int i=(j++,j+2,j+3); but when we are dealing with this then why the answer of our question becomes 5. as it should be 4 according to particular logic
@user3102208 this is equivalent to i = j + 3
I've read it but as j=1 and as you say i=j+3; so i should be 4 according to this. butt its not so its 5.
@user3102208 Instead try yourself, According to your expression int i = (j++, j+2, j+3); (in first comment) if j = 1 first j++ incremented to 2 then because of i = j + 3, i will be 5
@user3102208 Read last statement in answer I think that will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.