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

Semicolon is a statement terminator.

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-23 17:55

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

查看更多
三岁会撩人
4楼-- · 2019-03-23 18:00

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.

查看更多
forever°为你锁心
5楼-- · 2019-03-23 18:04

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.

查看更多
我命由我不由天
6楼-- · 2019-03-23 18:10

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

查看更多
登录 后发表回答