3

I know that C & C++ both are different languages, today I make a little typo in the following program but the program compiles fine on various C++ compilers (g++,clang,MSVC++)

Consider following program:

int main()
{
    int s[]={3,6,9,12,18};
    int* p=+s;  // Observe this strange looking initialization due to use of unary + operator
}

The above program compiles fine in C++ (See live demo here) but not in C (See live demo here.) My compiler ( gcc 4.8.1 ) gives me following error when I compile it as a C program.

[Error] wrong type argument to unary plus

What purpose the unary plus operator serves here? What it does exactly here? Why it is not allowed in C?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 3
    @JoachimPileborg: I already expected this kind of comments saying that "C and C++ are totally different languages " that's why I already mentioned this statement in my question. – Destructor Feb 01 '16 at 14:57
  • Yeah I saw that just a *little* to late. Sorry. – Some programmer dude Feb 01 '16 at 14:58
  • 1
    @JoachimPileborg I think the OP understands that. Nevertheless it is interesting to know why this is treated differently in C and C++. – Axel Feb 01 '16 at 14:58
  • And `for i in [1,2,3]: ...` runs fine in Python, but not in C. Why do you think different languages have the same syntax and semantics? – too honest for this site Feb 01 '16 at 14:59
  • 3
    Possible duplicate of [What is the purpose of the unary '+' operator in C?](http://stackoverflow.com/questions/6637005/what-is-the-purpose-of-the-unary-operator-in-c) – Eric Renouf Feb 01 '16 at 14:59
  • @Axel: The actual question is: why do you expect two different languages to behave to same? – too honest for this site Feb 01 '16 at 15:00
  • @JoachimPileborg: why this has been marked as duplicate? This is different question than linked question. – Destructor Feb 01 '16 at 15:04
  • @Axel: exactly that what I want to know. – Destructor Feb 01 '16 at 15:04
  • 1
    Better duplicate: https://stackoverflow.com/questions/25701381/what-is-the-purpose-of-unary-plus-operator-on-char-array – user5867440 Feb 01 '16 at 15:05
  • 2
    @Olaf Because C and C++ are very closely related. Even Stroustrup's FAQ still has the quote "Thus, C++ is as much a superset of ANSI C as ANSI C is a superset of K&R C and much as ISO C++ is a superset of C++ as it existed in 1985". So, if C and C++ differ in this regard, it rather was by intention and not by chance. It's another thing with all the other C-like languages that borrow the C-syntax but are not so closely related. – Axel Feb 01 '16 at 16:12
  • Note: In original C,`=+` did the same as `+=`. – chux - Reinstate Monica Feb 01 '16 at 16:12
  • @Axel: C is very well standardised as C11 in ISO/IEC 9899:2011. And with the release of the previous version (C99) ca. 17 years ago some semantics have changed. In general, identical syntax does not imply identical semantics - as we can see here. Also C has features C++ does not provide. Just read the standards and/or do some research. – too honest for this site Feb 01 '16 at 16:20

4 Answers4

7

The section 6.5.3.3 of C99 states:

1) The operand of the unary + or - operator shall have arithmetic type; ......

2) The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.

Pointers or arrays are not an arithmetic type.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
6

What purpose the unary plus operator serves here?

In C++, the behavior is laid out in section 5.3.1:

[2] The result of each of the following unary operators is a prvalue.

[7] The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument.

Sometimes people use this operator to "force" decay, i.e. in this case an array to pointer. But it is rather redundant since the conversion happens automatically.

What it does exactly here? Why it is not allowed in C?

Because the special meaning simply does not exist.

Community
  • 1
  • 1
user5867440
  • 130
  • 5
0

s will be treated as a pointer not as an array when + operator is called.

For C, it seems that this concept was not introduced yet.

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
0

I have only ever seen this used in situations where the unary + operator has been overloaded in an OOP language like C++. It hasn't got a purpose in C apart from possible causing errors like this where a pointer was accidentally passed to a macro or a function that does not expect one.

Either way it's a nasty way to write code, if there was a good reason for it then it should be accompanied by an explanatory comment from the original author.