Output of the simple question [modified]
-
Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.
"We can't solve problems by using the same kind of thinking we used when we created them"
modified on Monday, June 8, 2009 6:08 AM
-
Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.
"We can't solve problems by using the same kind of thinking we used when we created them"
modified on Monday, June 8, 2009 6:08 AM
IMHO
VC++
answer makes sense.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.
"We can't solve problems by using the same kind of thinking we used when we created them"
modified on Monday, June 8, 2009 6:08 AM
int a = 1;
a += ( a+= 3, 5, a );
a += ( a+= 3 ); //(Anything after the first comma will be ignored by the compiler. See Sequential evaluation operator[^]
a += ( a = a+3) //but a was delcared 1, so the expression a=1+3 makes RHS value as 4.
a += a; //But a is 4 now before executing this statement.
//Therefore adding another 4, the resultant a = 8. Makes perfect sense to me.shubhi wrote:
In AS/400’s C showing output: 5
Then that compiler sucks.
It is a crappy thing, but it's life -^ Carlo Pallini
-
IMHO
VC++
answer makes sense.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]It's a correct answer, but maybe not what was intended. It seems to be evaluating it as ((a+=3), 5, a) and returning the new value of 'a' (the AS/400 compiler returns the old value of 'a' I think). I'll have to find a precedence table, as I'd have expected it to be (a += (3, 5, a)), which would give a different result (4)... edit : looks like the assignment has higher precedence, according to MSDN. You learn something new every day :)
There are three kinds of people in the world - those who can count and those who can't...
-
int a = 1;
a += ( a+= 3, 5, a );
a += ( a+= 3 ); //(Anything after the first comma will be ignored by the compiler. See Sequential evaluation operator[^]
a += ( a = a+3) //but a was delcared 1, so the expression a=1+3 makes RHS value as 4.
a += a; //But a is 4 now before executing this statement.
//Therefore adding another 4, the resultant a = 8. Makes perfect sense to me.shubhi wrote:
In AS/400’s C showing output: 5
Then that compiler sucks.
It is a crappy thing, but it's life -^ Carlo Pallini
But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.
"We can't solve problems by using the same kind of thinking we used when we created them"
-
But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.
"We can't solve problems by using the same kind of thinking we used when we created them"
shubhi wrote:
then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.
You're right there.
shubhi wrote:
a += ( a+= 3, 5, a ); //a=1
Yes, the value of
a
(the last within the braces) is1
, so adding3
to it makes it4
. And then there is ana+=a
. Therefore 8.It is a crappy thing, but it's life -^ Carlo Pallini
modified on Monday, June 8, 2009 7:04 AM
-
shubhi wrote:
then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.
You're right there.
shubhi wrote:
a += ( a+= 3, 5, a ); //a=1
Yes, the value of
a
(the last within the braces) is1
, so adding3
to it makes it4
. And then there is ana+=a
. Therefore 8.It is a crappy thing, but it's life -^ Carlo Pallini
modified on Monday, June 8, 2009 7:04 AM
Rajesh R Subramanian wrote:
a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.
I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Rajesh R Subramanian wrote:
a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.
I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Yes, that was subtle indeed. Modified my answer. :-O
It is a crappy thing, but it's life -^ Carlo Pallini
-
Rajesh R Subramanian wrote:
a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.
I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Just to add For eg we have e1 , e2 Here ',' act as operator which will ``evaluate the subexpression e1, then evaluate e2; the value of the expression is the value of e2. So for a+=(a+=3,5,a) it will have as:- a+=(4,5,4)// since a=a+3 So in all a=8. But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.
-
But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.
"We can't solve problems by using the same kind of thinking we used when we created them"
Hello, I modified a portion of my previous answer. To be specific,
shubhi wrote:
a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.
That is correct. :)
It is a crappy thing, but it's life -^ Carlo Pallini
-
Just to add For eg we have e1 , e2 Here ',' act as operator which will ``evaluate the subexpression e1, then evaluate e2; the value of the expression is the value of e2. So for a+=(a+=3,5,a) it will have as:- a+=(4,5,4)// since a=a+3 So in all a=8. But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.
Anoop Dashora wrote:
But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.
You could generate the assembly and have a look at it, couldn't you? [added] Probably you can't, since you're not the OP... :-D [/added]
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.
"We can't solve problems by using the same kind of thinking we used when we created them"
modified on Monday, June 8, 2009 6:08 AM
Who cares what the answer is or should be or whatever - the lesson you should be learning from this question is just don't do this sort of thing. [edit]To expand on that - whenever some part of a language specification is defined as "implementation specific" or (as I think the C++ Standard says for cases like yours) "undefined", especially when it's language semantics like this, be afraid, be very afraid, and leave it well alone, as far as you are able.[/edit]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
modified on Monday, June 8, 2009 8:30 AM