Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC define unary operator '&&' instead of just using '&'?

Tags:

As discussed in this question, GCC defines nonstandard unary operator && to take the address of a label.

Why does it define a new operator, instead of using the existing semantics of the & operator, and/or the semantics of functions (where foo and &foo both yield the address of the function foo())?

like image 405
Jeremy Avatar asked Apr 28 '15 07:04

Jeremy


People also ask

Why is it called a unary operator?

In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function f : A → A, where A is a set. The function f is a unary operation on A.

Why ++ is a unary operator?

Unary Increment (++) In this type, the Unary Operator is denoted by the '++' sign. It increases the value by 1 to the operand and then stores the value to the variable. It works for both Prefix and Postfix positions.

What unary operator means?

What Does Unary Operator Mean? A unary operator, in C#, is an operator that takes a single operand in an expression or a statement.

Which is the unary logical operator?

Unary Logical Operator The ! (logical negation) operator produces the value 0 if its operand is true (nonzero) and the value 1 if its operand is false (0).


1 Answers

Label names do not interfere with other identifiers, because they are only used in gotos. A variable and a label can have the same name, and in standard C and C++ it's always clear from the context what is meant. So this is perfectly valid:

name:   int name;   name = 4; // refers to the variable   goto name; // refers to the label 

The distinction between & and && is thus needed so the compiler knows what kind of name to expect:

  &name; // refers to the variable   &&name; // refers to the label 
like image 95
Sebastian Redl Avatar answered Oct 26 '22 00:10

Sebastian Redl