在C语言(也是C ++)中,如何“&"运算符既可以用作地址运算符,又可以按位运算符?由于C不支持运算符重载 [英] In C (also C++), how '&' operator works as both address operator and bitwise operator ? As operator overloading is not supported by C

查看:96
本文介绍了在C语言(也是C ++)中,如何“&"运算符既可以用作地址运算符,又可以按位运算符?由于C不支持运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运算符&"可以通过以下两种方式使用 int a;scanf(%d",& a); printf(%d",1& 2).但是行为不同(第一次是作为地址运算符,第二次是按位运算符).我知道C中不存在运算符重载.然后它如何工作?还要突出显示c ++.

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++.

推荐答案

在"C"中在语言中,当运算符用作表达式的前缀,表达式的后缀或中缀"时,它们具有不同的含义.(在两个表达式之间).

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.

以相同的方式,"C"表示编译器知道'&'是否是按位的,或基于其位置的是表达式:如果在两个表达式之间,则为AND,如果在表达式之前,则为"address-of".

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'.

有关中缀的信息,请参见 https://en.wikipedia.org/wiki/Infix_notation .

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

这篇关于在C语言(也是C ++)中,如何“&"运算符既可以用作地址运算符,又可以按位运算符?由于C不支持运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆