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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Semicolon denotes sequential composition. It is also used to delineate declarations.
The semicolon is a terminal, a token that terminates something. What exactly it terminates depends on the context.
';'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:
This (horrible) bit of code, called with no arguments such that
argc
is1
, prints: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 tof()
printingf()\n
?Because... (drumroll please)... in C++ adjacent string literals are concatenated, so the program's parsed like this:
What this does is:
[argc/1]
(second) character in "message\0\1\0\1\1", which is the first 'e'std::cout
(printing it)std::cout
tobool
which producestrue
(because the printing presumably worked), sof()
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.
It is part of the syntax and therein element of several statements. In EBNF:
This list is not complete. Please see my comment.
The semicolon is a punctuator, see 2.13 §1
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):
expression-statement
iteration-statement
jump-statement
ssimple-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.