简体   繁体   English

C++中的分号是什么?

[英]What is the semicolon in C++?

Roughly speaking in C++ there are:粗略地说,在 C++ 中有:

  • operators ( + , - , * , [] , new , ... )运算符 ( + , - , * , [] , new , ... )
  • identifiers (names of classes, variables, functions,...)标识符(类名、变量名、函数名……)
  • const literals ( 10 , 2.5 , "100" , ...) const 文字 ( 10 , 2.5 , "100" , ...)
  • some keywords ( int , class , typename , mutable , ...)一些关键字( intclasstypenamemutable ,...)
  • brackets ( { , } , < , > )方括号 ( { , } , < , > )
  • preprocessor ( # , ## ...).预处理器 ( # , ## ...)。

But what is the semicolon?但是分号是什么?

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++ 程序的词法表示包括许多预处理标记,这些标记用于预处理器的语法中或转换为运算符和标点符号的标记

It is part of the syntax and therein element of several statements.它是几个语句的语法和元素的一部分。 InEBNF :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.请看我的评论。

The semicolon is a terminal, a token that terminates something.分号是一个终结符,一个终止某些事物的标记。 What exactly it terminates depends on the context.它究竟终止什么取决于上下文。

分号是语句终止符。

Semicolon denotes sequential composition.分号表示顺序组合。 It is also used to delineate declarations.它还用于描述声明。

The semicolon isn't given a specific name in the C++ standard. C++ 标准中没有给分号一个特定的名称。 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++ 语法的以下部分(不一定是完整列表)的末尾:

  • an expression-statement expression-statement
  • a do/while iteration-statement do/while iteration-statement
  • the various jump-statement s各种jump-statement
  • the simple-declaration simple-declaration

Note that in an expression-statement , the expression is optional.请注意,在expression-statement ,表达式是可选的。 That's why a 'run' of semicolons, ;;;;这就是为什么“运行”的分号, ;;;; , is valid in many (but not all) places where a single one is. , 在许多(但不是所有)有一个的地方都有效。

';'s are often used to delimit one bit of C++ source code, indicating it's intentionally separate from the following code. ';'s 通常用于分隔 C++ 源代码的一位,表示它有意与以下代码分开。 To see how it's useful, let's imagine we didn't use it:为了看看它有什么用处,让我们假设我们没有使用它:

For example:例如:

#include <iostream>

int f() { std::cout << "f()\n"; }
int g() { std::cout << "g()\n"; }

int main(int argc)
{
    std::cout << "message"

    "\0\1\0\1\1"[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:这个(可怕的)代码,不带参数调用,使得argc1 ,打印:

ef()\n

Why not "messagef()\\n"?为什么不是“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 ?这就是首先给定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++ 中相邻的字符串文字是连接的,所以程序的解析如下:

std::cout << "message\0\1\0\1\1"[argc] ? f() : g();

What this does is:它的作用是:

  • find the [argc/1] (second) character in "message\\0\\1\\0\\1\\1", which is the first 'e'在“message\\0\\1\\0\\1\\1”中找到[argc/1] (第二个)字符,它是第一个'e'
  • send that 'e' to std::cout (printing it)将该 'e' 发送到std::cout (打印它)
  • the ternary operator '?'三元运算符 '?' triggers casting of std::cout to bool which produces true (because the printing presumably worked), so f() is called...!触发std::coutbool ,产生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.这是一个复杂的例子,说明 C++ 代码如何使用和不使用 ';' 来改变含义。 I'm sure if I or other readers think on it for a few minutes we could come up with other - and simpler - examples.我敢肯定,如果我或其他读者思考几分钟,我们可以想出其他——更简单——的例子。

Anyway, the ';'无论如何,';' is necessary to inform the compiler that statement termination/separation is intended.必要通知编译器语句终止/分离的意图。

分号让编译器知道它已到达命令 AFAIK 的末尾。

The semicolon (;) is a command in C++.分号 (;) 是 C++ 中的命令。 It tells the compiler that you're at the end of a command.它告诉编译器你在命令的末尾。

If I recall correctly, Kernighan and Ritchie called it punctuation.如果我没记错的话,Kernighan 和 Ritchie 称之为标点符号。 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, eg:运算符和其他标点符号之间的区别有些人为,但在 C 或 C++ 的上下文中很有用,因为某些标记( ,=: )可以是运算符或标点符号,具体取决于上下文,例如:

f( 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

In the case of the first two, the distinction is important, since a user defined overload will only affect the operator, not punctuation.在前两种情况下,区别很重要,因为用户定义的重载只会影响运算符,而不影响标点符号。

It represents the end of a C++ statement.它代表 C++ 语句的结束。

For example,例如,

 int i=0;
 i++;

In the above code there are two statements.在上面的代码中有两个语句。 The first is for declaring the variable and the second one is for incrementing the value of variable by one.第一个用于声明变量,第二个用于将变量的值加一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM