-->

What is the semicolon in C++?

2019-03-23 17:15发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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



回答4:

Semicolon is a statement terminator.



回答5:

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



回答6:

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.



回答7:

The semicolon lets the compiler know that it's reached the end of a command AFAIK.



回答8:

The semicolon (;) is a command in C++. It tells the compiler that you're at the end of a command.



回答9:

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



回答10:

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

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.



回答11:

It represents the end of a C++ statement.

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.



标签: c++ semicolon