C++中的分号是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6464436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the semicolon in C++?
提问by Mihran Hovsepyan
Roughly speaking in C++ there are operators (+
, -
, *
, []
, new
, ...
), identifiers (names of classes, variables, functions,...), const literals (10
, 2.5
, "100"
, ...), some keywords (int
, class
, typename
, mutable
, ...), brackets ({
, }
, <
, >
), preprocessor (#
, ##
...). But what is the semicolon?
粗略地说,在 C++ 中有运算符(+
, -
, *
, []
, new
, ...
)、标识符(类名、变量名、函数名...)、常量字面量(10
, 2.5
, "100"
, ...),一些关键字(int
, class
, typename
, mutable
, .. .)、方括号 ( {
, }
, <
, >
), 预处理器 ( #
, ##
...)。但是分号是什么?
采纳答案by fredoverflow
The semicolon is a punctuator, see 2.13 §1
分号是一个标点符号,见 2.13 §1
The lexical representation of C++ programs includes a number of preprocessing tokens which are used in the syntax of the preprocessor or are converted into tokens for operators and punctuators
C++ 程序的词法表示包括许多预处理标记,这些标记用于预处理器的语法中或转换为运算符和标点符号的标记
回答by Hyperboreus
It is part of the syntax and therein element of several statements. In EBNF:
它是几个语句的语法和元素的一部分。在EBNF 中:
<do-statement>
::= 'do' <statement> 'while' '(' <expression> ')' ';'
<goto-statement>
::= 'goto' <label> ';'
<for-statement>
::= 'for' '(' <for-initialization> ';' <for-control> ';' <for-iteration> ')' <statement>
<expression-statement>
::= <expression> ';'
<return-statement>
::= 'return' <expression> ';'
This list is not complete. Please see my comment.
这份清单并不完整。请看我的评论。
回答by Xeo
The semicolon is a terminal, a token that terminates something. What exactly it terminates depends on the context.
分号是一个终结符,一个终止某些事物的标记。它究竟终止什么取决于上下文。
回答by zvrba
Semicolon is a statement terminator.
分号是语句终止符。
回答by Mikola
Semicolon denotes sequential composition. It is also used to delineate declarations.
分号表示顺序组合。它还用于描述声明。
回答by Michael Burr
The semicolon isn't given a specific name in the C++ standard. It's simply a character that's used in certain grammar productions (and it just happens to be at the end of them quite often, so it 'terminates' those grammatical constructs). For example, a semicolon character is at the end of the following parts of the C++ grammar (not necessarily a complete list):
C++ 标准中没有给分号一个特定的名称。它只是在某些语法产生式中使用的一个字符(它恰好经常出现在它们的末尾,因此它“终止”了那些语法结构)。例如,分号字符位于 C++ 语法的以下部分(不一定是完整列表)的末尾:
- an
expression-statement
- a do/while
iteration-statement
- the various
jump-statement
s - the
simple-declaration
- 一个
expression-statement
- 做/一会儿
iteration-statement
- 各种
jump-statement
s - 这
simple-declaration
Note that in an expression-statement
, the expression is optional. That's why a 'run' of semicolons, ;;;;
, is valid in many (but not all) places where a single one is.
请注意,在 an 中expression-statement
,表达式是可选的。这就是为什么“运行”分号, ;;;;
, 在许多(但不是所有)只有一个分号的地方都有效。
回答by Tony Delroy
';'s are often used to delimit one bit of C++ source code, indicating it's intentionally separate from the following code. To see how it's useful, let's imagine we didn't use it:
';'s 通常用于分隔 C++ 源代码的一位,表示它有意与以下代码分开。为了看看它有什么用处,让我们假设我们没有使用它:
For example:
例如:
#include <iostream>
int f() { std::cout << "f()\n"; }
int g() { std::cout << "g()\n"; }
int main(int argc)
{
std::cout << "message"
"ef()\n
std::cout << "messagef( a, b ); // comma is punctuation
f( (a, b) ); // comma is operator
a = b; // = is assignment operator
int a = b; // = is punctuation
x = c ? a : b; // colon is operator
label: // colon is punctuation
##代码##"[argc] ? f() : g();
"[argc] ? f() : g(); // final ';' needed to make this compile
// but imagine it's not there in this new
// semicolon-less C++ variant....
}
This (horrible) bit of code, called with no arguments such that argc
is 1
, prints:
这个代码(可怕的)位,称为不带参数,从而argc
为1
,打印:
Why not "messagef()\n"? That's what might be expected given first std::cout << "message"
, then "\0\1\0\1\1"[1]
being '\1'
- true
in a boolean sense - suggests a call to f()
printing f()\n
?
为什么不是“messagef()\n”?这就是首先给出的预期std::cout << "message"
,然后"\0\1\0\1\1"[1]
是'\1'
-true
在布尔意义上 - 建议调用f()
打印f()\n
?
Because... (drumroll please)... in C++ adjacent string literals are concatenated, so the program's parsed like this:
因为...(请打鼓)...在 C++ 中相邻的字符串文字是连接在一起的,所以程序的解析如下:
##代码##What this does is:
它的作用是:
- find the
[argc/1]
(second) character in "message\0\1\0\1\1", which is the first 'e' - send that 'e' to
std::cout
(printing it) - the ternary operator '?' triggers casting of
std::cout
tobool
which producestrue
(because the printing presumably worked), sof()
is called...!
[argc/1]
在“message\0\1\0\1\1”中找到(第二个)字符,也就是第一个'e'- 将该“e”发送到
std::cout
(打印) - 三元运算符 '?' 触发投射
std::cout
到bool
which 产生true
(因为打印可能有效),所以f()
被称为......!
Given this string literal concatenation is incredibly useful for specifying long strings (and even shorter multi-line strings in a readable format), we certainly wouldn't want to assume that such strings shouldn't be concatenated. Consequently, if the semicolon's gone then the compiler must assume the concatenation is intended, even though visually the layout of the code above implies otherwise.
鉴于此字符串文字连接对于指定长字符串(甚至可读格式的较短多行字符串)非常有用,我们当然不想假设不应连接此类字符串。因此,如果分号消失了,那么编译器必须假设连接是有意的,即使上面代码的布局在视觉上暗示了其他情况。
That's a convoluted example of how C++ code with and with-out ';'s changes meaning. I'm sure if I or other readers think on it for a few minutes we could come up with other - and simpler - examples.
这是一个令人费解的示例,说明带有和不带有 ';' 的 C++ 代码如何改变含义。我敢肯定,如果我或其他读者思考几分钟,我们可以想出其他——更简单——的例子。
Anyway, the ';' is necessaryto inform the compiler that statement termination/separation is intended.
无论如何,';' 有必要通知编译器语句终止/分离的意图。
回答by James Kanze
If I recall correctly, Kernighan and Ritchie called it punctuation.
Technically, it's just a token (or terminal, in compiler-speak), which
can occur in specific places in the grammar, with a specific semantics
in the language. The distinction between operators and other punctuation
is somewhat artificial, but useful in the context of C or C++, since
some tokens (,
, =
and :
) can be either operators or punctuation,
depending on context, e.g.:
如果我没记错的话,Kernighan 和 Ritchie 称之为标点符号。从技术上讲,它只是一个标记(或终端,用编译器的话说),它可以出现在语法中的特定位置,在语言中具有特定的语义。运算符和其他标点符号之间的区别有点人为,但在 C 或 C++ 的上下文中很有用,因为某些标记(,
,=
和:
)可以是运算符或标点符号,具体取决于上下文,例如:
In the case of the first two, the distinction is important, since a user defined overload will only affect the operator, not punctuation.
在前两种情况下,区别很重要,因为用户定义的重载只会影响运算符,而不是标点符号。
回答by Sapiens
The semicolon lets the compiler know that it's reached the end of a command AFAIK.
分号让编译器知道它已到达命令 AFAIK 的末尾。
回答by cprogrammer
The semicolon (;) is a command in C++. It tells the compiler that you're at the end of a command.
分号 (;) 是 C++ 中的命令。它告诉编译器你在命令的末尾。