Look up the definition of the pre-increment and post-increment operators in
C. They are right associative, which means that they operate to the right
and as they are unary they have higher precedence than the binary "+"
operator when they are the operands for the "+" operator. So the instruction
sequence you get for your three piece combo with two binary "+" operations
and three pre-increments is:
INC CNT ; CNT = 4
INC CNT ; CNT = 5
LD CNT ; 5
ADD CNT ; 10
INC CNT ; CNT=6
ADD CNT ; 16
; Et voila.
#include <stdio.h>
int
main(int cnt, char * argc[])
{
int c, c1;
c = 3;
printf("Expr is %d\n, c is %d\n\n", ++c + ++c, c);
c = 3;
printf("Expr is %d\n, c is %d\n\n", ++c + ++c + ++c, c);
c = 3;
printf("Expr is %d\n, c is %d\n\n", c++ + c++, c);
}
You are just confused over operator sequencing and precedence.
Jim
VK - if you are getting 18, then your system has a bug!! This sequence will
not optimize to a multiplication because of the operator precedence
requirements.
From: [email protected] [mailto:[email protected]] On Behalf Of [
Xze ]
Sent: Thursday, September 02, 2010 8:02 AM
To: [email protected]
Subject: Re: Pre-increment
why you had 16, I don't understand. Typo?
No, checked that again, its 16
And how i'm curious about the result = 10; Is (3 + 1 + 1) * 2 a valid
computation?! The second pre-increment will be applied on an already
pre-incremented value
As far as i know it should look like that:
CNT = 3
RES = (3+1) + ((3+1)+1) = 9
Regards,
Xze
On Thu, Sep 2, 2010 at 13:15, VK <[email protected]> wrote:
Hi,
tried that on TAFC R10 (Change 89685)
Source:
001 PROGRAM KZM.PLUS
002
003 CNT = 3
004 PRINT ++CNT + ++CNT
005
006 CNT2 = 3
007
008 PRINT ++CNT2 + ++CNT2 + ++CNT2
009
010 STOP
011 END
Result:
10
18
It looks that preincrement has preference and it's proceeded in full
before summing ttakes place:
(3+1+1) * 2 = 10
(3+1+1+1) * 3 = 18
why you had 16, I don't understand. Typo?
VK
On Sep 1, 4:34 pm, "[ Xze ]" <[email protected]> wrote:
> Hi all!
>
> My question pertains to the way that jbase treats pre-increments
>
> Consider the following routine:
>
> 0004 PROGRAM MY.MATH
> 0005
> 0006 CNT = 3
> 0007 RES = ++CNT + ++CNT
> 0008
> 0009 PRINT 'RES = ':RES
> 0010 RETURN
> 0011 END
>
> The output result is RES = 10
>
> if i change line 007 to RES = ++CNT + ++CNT + ++CNT,
> then the output result is RES = 16
>
> Can anybody explain this behaviour? (I expected the first case to output
RES
> = 9 and the second RES = 15)
>
> My system configuration:
>
> OS : AIX 5.3
> jbase : Major 5.0 , Minor 20 , Patch 0364 (Change 85159)
>
> Regards
--
Please read the posting guidelines at:
http://groups.google.com/group/jBASE/web/Posting%20Guidelines
IMPORTANT: Type T24: at the start of the subject line for questions specific
to Globus/T24
To post, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit this group at
http://groups.google.com/group/jBASE?hl=en
--
Please read the posting guidelines at:
http://groups.google.com/group/jBASE/web/Posting%20Guidelines
IMPORTANT: Type T24: at the start of the subject line for questions specific
to Globus/T24
To post, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit this group at
http://groups.google.com/group/jBASE?hl=en
--
Please read the posting guidelines at:
http://groups.google.com/group/jBASE/web/Posting%20Guidelines
IMPORTANT: Type T24: at the start of the subject line for questions specific to
Globus/T24
To post, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en