int x = 2;
int y = 5;
int z = x +++ y;
printf("%d",z);
Both VC++ and GCC give 7 as output. My confusion here is, it could be x++ + y, or x + ++y. Is this defined?
int x = 2;
int y = 5;
int z = x +++ y;
printf("%d",z);
Both VC++ and GCC give 7 as output. My confusion here is, it could be x++ + y, or x + ++y. Is this defined?
In both C and C++, the principle of lexical analysis is, the longest sequence of characters that can form a valid token is taken as one (also known as "maximal munch"). So x+++y
is unambiguously parsed as (x++) + y
.
2.4/(3.3) -- Otherwise, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail.
According to maximal munch rule compiler always interpret x +++ y
as x++ + y
and therefore behaviour is well defined.
p(4)
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.[...]
p(6)
EXAMPLE 2 The program fragment
x+++++y
is parsed asx ++ ++ + y
, which violates a constraint on increment operators, even though the parsex ++ + ++ y
might yield a correct expression.