What is the semicolon in C++?

2019-03-23 17:05发布

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++ semicolon
11条回答
我命由我不由天
2楼-- · 2019-03-23 17:45

Semicolon denotes sequential composition. It is also used to delineate declarations.

查看更多
我命由我不由天
3楼-- · 2019-03-23 17:48

The semicolon is a terminal, a token that terminates something. What exactly it terminates depends on the context.

查看更多
Explosion°爆炸
4楼-- · 2019-03-23 17:49

';'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:

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:

ef()\n

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?

Because... (drumroll please)... in C++ adjacent string literals are concatenated, so the program's parsed like this:

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'
  • send that 'e' to std::cout (printing it)
  • the ternary operator '?' triggers casting of std::cout to bool which produces true (because the printing presumably worked), so f() is called...!

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.

Anyway, the ';' is necessary to inform the compiler that statement termination/separation is intended.

查看更多
Viruses.
5楼-- · 2019-03-23 17:50

It is part of the syntax and therein element of several statements. In 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.

查看更多
干净又极端
6楼-- · 2019-03-23 17:53

The semicolon is a punctuator, see 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

查看更多
疯言疯语
7楼-- · 2019-03-23 17:53

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):

  • an expression-statement
  • a do/while iteration-statement
  • the various jump-statements
  • the 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.

查看更多
登录 后发表回答