In C (also C++), how '&' operator works as both address operator and bitwise operator ? As operator overloading is not supported by C

一个人想着一个人 提交于 2021-01-28 03:28:10

问题


The operator '&' can be used in both of following way int a; scanf("%d",&a); and printf("%d",1&2). But different behaviour (for first as address operator and second time as bit-wise operator). I know operator overloading is not there in C. Then how it works ?. Also highlight for c++.


回答1:


In "C" language, operators have different meaning when they are used as prefix to expression, suffix to expression or "infix" (between two expressions).

Consider '*', which performs multiplication as 'infix' operator, and pointer indirection when used as a prefix. Similarily, the '-' operator, which performs subtraction as 'infix' operator, and negation when used as a prefix.

Basically, it's not about overriding, it if the operator appears between two expressions, or as a prefix to a single expression.

In the same way, The "C" compiler knows if the '&' is bit-wise and, or address-of, based on it's position is the expression: If it is between two expressions, it's the AND, if it is before an expression, it is 'address-of'.

See https://en.wikipedia.org/wiki/Infix_notation about infix.




回答2:


I know operator overloading is not there in C.

This is incorrect. a + b performs integer addition if a and b are integers, floating-point addition of a and b are floating-point numbers, and pointer arithmetic if a or b is a pointer.

C has operator overloading built into the language. It does not support custom operator overloading defined by the program.

In the case of & being an operator for taking an address and for performing a bitwise AND, the distinction is made by the language grammar. The & for taking an address can appear only applied to a cast-expression in the grammar. The & for bitwise AND can appear only after an AND-expression and before an equality-expression. These “tokens” (cast-expression, AND-expression, and equality-expression) may be unfamiliar to you, but they are formally defined in the grammar for the C language, and, as the compiler is parsing source code, it recognizes the structure of expressions and matches the source code to the tokens of the grammar. This is also true for C++ except for a minor technical difference: In C++, one of the tokens is and-expression instead of AND-expression.

The definition of the grammar is such that recognition of these tokens always uniquely distinguishes how the & operator is being used.




回答3:


C does not support operator overloading (beyond what it built into the language). As you can see in this Wikipedia Operators in C

The Address-of ("address of a") "&a" is defined as R* K::operator &(); whereas

The Bitwise AND "a & b" is defined as R K::operator &(S b);

So basically the "&" operator has different meaning when used as a unary operator and as a binary operator operator. The same goes for various other operators like, "*" , "-", etc.




回答4:


It has simple different meanings when applied to the lvalue (it the unary operator in this case) or when it is used in the math expression with two operands.




回答5:


"The operator & can be used in both of following way int a; scanf("%d",&a); and printf("%d",1&2)."

Note that in the case of C++ you forgot another important third use. The & operator can also be used to declare references.

int x = 24;
int& r_x = x;



回答6:


the language divides operators based on its operands first. In one category itself overloading can be in-built. Your example is about the first division. Address-of is a unary operator and bitwise-AND is a binary operator. when you write operator function in c++ you will see the difference of these two categories. Operator overloading is inbuilt to languages. example simple arithmetic addition operator. it can work with simple one-byte integer data as well as float (significant & exponent). Basically it is there with maths. so while making C language, they just translated those into functionality. In C specification, you cannot find overloading as a keyword for this behavior. According to them, after formula expression, anything has to be expressed as different functions. Each function should be named based on the functionality that it offers. When C++ introduced an opportunity to create new types, operators with its basic n-nary form allowed to operate with new types. In a nutshell, C's philosophy was different.



来源:https://stackoverflow.com/questions/63099784/in-c-also-c-how-operator-works-as-both-address-operator-and-bitwise-ope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!