Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of expression "+ + a"?

Tags:

c

When there is a space between + +, what is meaning of expression "+ + a". How is this expression evaluated?

    int main()
    {
        int a = 3;

        printf("%d %d", + +a, a);
    }

and also how is a+++a evaluated? Is it undefined or unspecified or implementation defined?

like image 704
dev2 Avatar asked Sep 23 '13 21:09

dev2


People also ask

What is means of expression?

1. : the act of making your thoughts, feelings, etc., known by speech, writing, or some other method : the act of expressing something. [noncount] freedom of expression [=freedom to say and show what you feel and believe] Dance is a form of artistic/creative expression.


2 Answers

It is a no-op — twice because + a is a no-op and it is repeated.

a+++a is unambiguously parsed as a++ + a, but leads to undefined behaviour when executed.

Note that if the code set a = -3;, the value printed would still be -3, twice.

like image 95
Jonathan Leffler Avatar answered Sep 27 '22 20:09

Jonathan Leffler


When there is a space in the middle in the ++ operator, then you are just applying the unary plus operator twice.

About the expression a+++a, the C specification says that when there is such an ambiguity, munch as much as possible (the "greedy lexer" or "maximal munch" rule). So a+++a is evaluated as a++ + a

According to that rule, an expression like, z = y+++++x; will be parsed as z = y++ ++ +x;, which is invalid (the result of a post-increment is not itself incrementable).

like image 38
Moha the almighty camel Avatar answered Sep 27 '22 21:09

Moha the almighty camel