Todd Carnes
8/15/2011 2:27:00 AM
On Sun, 14 Aug 2011 15:10:48 -0400, James Kuyper wrote:
> On 08/14/2011 02:56 PM, LJ wrote:
>> int a = 2;
>> int b = 3;
>> c = a+++b;
>> What will be the value of c??
>> Shouldn't it be undefined depending upon the interpretation??
>
> The C standard mandates what's called the "maximal munch" rule; "If the
> input stream has been parsed into preprocessing tokens up to a given
> character, the next preprocessing token is the longest sequence of
> characters that could constitute a preprocessing token." (6.4p4)
>
> Therefore, there's only one permitted way of parsing the third
> statement:
>
> c = a++ + b;
>
> That's equivalent to the following statement, where I've inserted some
> unnecessary parenthesis to clarify the order of operations:
>
> c = ((a++) + b);
>
> Assuming that c has an arithmetic type, the behavior of that statement
> is well-defined: 'a' ends up with a value of 3, and c ends up with a
> value of 5 (converted, if necessary, to whatever the type of c happens
> to be).
Ok, I ran the code below and got 5, just like you said it would be. Keith
already explained why. I got the pre- and post- increment thing wrong.
Todd
#include <stdio.h>
int main(int argc, char **argv)
{
int a = 2;
int b = 3;
int c;
c = a+++b;
printf("c = %i", c);
return 0;
}