C 运算符 // 和 \/ 做了什么?
任何人都可以在 C 语言中“声明”自己的运算符……也就是说,如果一个人是 C 编译器大师并且拥有 C 编译器的源代码! ;-)
进一步令人困惑的问题:
- 这些操作是如何完成的 C99?海湾合作委员会? ...
- 为什么是 /\ & \/ 掉线了?
- /\ 和 \/ 运算符对哪些类型有效?
谷歌搜索“/\ \/”自然不会返回任何结果。维基百科上既没有 /\ 也没有 \/ 的页面。但我发现 XML 字符实体中内置了类似的运算符!
Source added: I found the offending example in the PDP's cc source file "c00.c":
/*
* Return the next symbol from the input.
* peeksym is a pushed-back symbol, peekc is a pushed-back
* character (after peeksym).
* mosflg means that the next symbol, if an identifier,
* is a member of structure or a structure tag or an enum tag
*/
symbol()
{
...
case BSLASH:
if (subseq('/', 0, 1))
return(MAX);
goto unkn;
case DIVIDE:
if (subseq('\\', 0, 1))
return(MIN);
if (subseq('*',1,0))
return(DIVIDE);
...
}
Actual Implementations: The /\ and \/ operators date back as far as Sixth Edition Unix 1975 (so far). Examples: Unix V6(1975), Unix V7(1979) and more currently BSD 2.11(1992-2008)
Anyone can "declare" ones own operators in C.... that is if one is a C compiler guru and has the source code to the C compiler! ;-)
Further questions to puzzle:
- How are these operations done in
C99? gcc? ... - And why were /\ & \/ dropped?
- Which types were the /\ and \/ operators valid for?
Googling for "/\ \/" naturally returns nothing. Wikipedia has a page for neither /\ nor \/. But I have spotted form similar operators are built into the XML character entities!
Source added: I found the offending example in the PDP's cc source file "c00.c":
/*
* Return the next symbol from the input.
* peeksym is a pushed-back symbol, peekc is a pushed-back
* character (after peeksym).
* mosflg means that the next symbol, if an identifier,
* is a member of structure or a structure tag or an enum tag
*/
symbol()
{
...
case BSLASH:
if (subseq('/', 0, 1))
return(MAX);
goto unkn;
case DIVIDE:
if (subseq('\\', 0, 1))
return(MIN);
if (subseq('*',1,0))
return(DIVIDE);
...
}
Actual Implementations:
The /\ and \/ operators date back as far as Sixth Edition Unix 1975 (so far). Examples: Unix V6(1975), Unix V7(1979) and more currently BSD 2.11(1992-2008)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
/\ 和 / 都没有被定义为 ISO C89 标准中的运算符,而且我认为它们在任何早期版本中都没有定义过。据我所知,C99 中肯定没有定义它们。
以下是 ANSI C89 标准草案,供参考:http://flash-gordon.me .uk/ansi.c.txt
(您可能是一些奇怪的神秘预处理器魔法的受害者)
Neither /\ nor / are defined as operators in the ISO C89 standard, and I don't think they were ever defined in any earlier version. And they are definitely not defined in C99 as far as I know.
Here's a draft of the ANSI C89 standard, for reference: http://flash-gordon.me.uk/ansi.c.txt
(You are likely a victim of some weird arcane preprocessor magic)
\/
看起来像sup
和/\
看起来像inf
。它们也可以是 ∨ 和分别为 ∧。
我不记得曾在 K&R 第二版或任何其他 C 书籍中看到过这些内容。
\/
looks likesup
and/\
looks likeinf
. They could also be ∨ and∧, respectively.
I don't remember ever seeing these in K&R 2nd edition or any other C book.
猜测!
如果它们周围有空格,则:
逻辑:预处理阶段必须处理反斜杠和后面的字符,并且很可能将反斜杠空格视为空格,将反斜杠-斜杠视为斜杠。
也就是说,SUN C 编译器(版本 12)和 GNU C 编译器(版本 4.4)都拒绝包含反斜杠的代码。但我很容易相信旧的、预标准的 C 预处理器对此不太谨慎。
标准合规性
运算符从未成为 C 正式版本的一部分。
此外,该标准不允许我给出的解释(第 5.1.1.2 节翻译阶段,在 C89 和 C99 中) - 但非标准编译器不受当然是标准。
在发布来源后添加:
有趣!因此,“
a = b /\ c;
”将 b 和 c 的最大值分配给 a,将“a = b \/ c;
”分配给 a 似乎是合理的。做最少的事情(或者,正如 Greg Hewgill 指出的那样,更可能反之亦然)。而且,在那些日子里,现代的“+=
”运算符很可能仍然写为“=+
”并且实际上是两个标记(没有支持此断言的证据;再次记忆失败),因此假设的“a =/\ b;
”(或者,用现代表示法,“a /\= b;
') 将是最大赋值运算符,等等。我还想到 Thompson 的 ACM 图灵奖演讲 '对信任信任的反思'在某种程度上是相关的。
Speculation!
If you have spaces around them, then:
Logic: the preprocessing phase has to deal with backslash and a character after, and is quite likely to treat backslash-space as space, and backslash-slash as slash.
That said, both the SUN C compiler (version 12) and GNU C compiler (version 4.4) reject code containing the backslash. But I could easily believe that old, pre-standard C preprocessors were less careful about it.
Standards compliance
The operators have never been part of an official version of C.
Also, the standard would not allow the interpretation I gave (section 5.1.1.2 Translation phases, in both C89 and C99) - but non-standard compilers are not constrained by the standard, of course.
Added after the source was posted:
Interesting! So it looks plausible for '
a = b /\ c;
' to assign the maxiumum of b and c to a, and 'a = b \/ c;
' to do the minimum (or, as Greg Hewgill pointed out, more likely vice versa). And, in those days, it was probable that the modern '+=
' operators were still written as '=+
'and were in fact two tokens(no supporting evidence for this assertion; failing memory again), so the hypothetical 'a =/\ b;
' (or, in modern notation, 'a /\= b;
') would have been the max-assignment operator, etc.It also occurs to me that Thompson's ACM Turing Award speech 'Reflections On Trusting Trust' is somehow relevant.
我不确定
\/
,但/\
是一个有效的构造。它用于将单行注释的两个斜杠放在不同的行上。例如:这是有效的,因为反斜杠字符转义了换行符,并且解析器继续执行,就好像它不存在一样。如果反斜杠后面有空格或者第二个正斜杠缩进,则此操作将不起作用。因此,可以根据需要转义任意多的换行符,如在
常规单行注释的末尾也可以使用反斜杠,以使它们继续到下一行,如
I'm not sure about
\/
, but/\
is a valid construct. It is used to place the two slashes of a single line comment on separate lines. For example:This works because the backslash character escapes the newline and the parser continues as if it wasn't there. This will not work if there is a space after the backslash or if the second forward slash is indented. Because of this, it is possible to escape as many newlines as you like, as in
Backslashes can also be used at the end of regular single line comments to make them continue to the next line, as in
C 从来没有这些运算符。
通常 / 在某些字符串系统中是转义码,不确定 /\ 是否有任何含义。
C has never had those operators.
Typically / would be an escape code in some string systems, not sure that /\ has ever had any meaning.
我怀疑它们是否有任何意义。如果他们曾经这样做过,那也是很久以前的事了。据我所知,从 C 语言中删除的唯一主要运算符是 =+ 和 =-,它们是 += 和 -= 的早期同义词。您可能想查看 DMR 的 Primeval C 页面为证据。
I doubt they ever meant anything. If they ever did, it was a long time ago. The only major operators I know of that have been removed from C were =+ and =-, which were early synonyms for += and -=. You might want to look at DMR's Primeval C Page for evidence.
我猜测这些是对形式符号逻辑的引用:
http://en.wikipedia。 org/wiki/Table_of_logic_symbols
/ 用于表示析取(OR)
/\ 用于(不太频繁)表示合取 (AND)
I'm going to guess that these are a reference to formal symbolic logic:
http://en.wikipedia.org/wiki/Table_of_logic_symbols
/ is used to denote disjunction (OR)
/\ is used (less frequently) to denote conjunction (AND)
插入符 (
^
) 执行按位异或。我不相信有“V”运算符。那是字母“V”(或者看起来非常像的东西)。有人可能想命名一个变量。
The Caret (
^
) performs a bitwise exclusive or.I don't believe there is a "V" operator. That's the letter 'V' (or something that looks a whole heck of a lot like it). Somebody might want to name a variable that.