Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11108

What is the semicolon in C++?

Roughly speaking in C++ there are:

But what is the semicolon?

Upvotes: 24

Views: 37331

Answers (11)

Nitin S
Nitin S

Reputation: 7600

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.

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32449

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.

Upvotes: 14

James Kanze
James Kanze

Reputation: 154037

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.

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263350

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

Upvotes: 21

Tony Delroy
Tony Delroy

Reputation: 106236

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

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340436

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.

Upvotes: 1

Xeo
Xeo

Reputation: 131877

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

Upvotes: 7

cprogrammer
cprogrammer

Reputation: 5675

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

Upvotes: 0

zvrba
zvrba

Reputation: 24584

Semicolon is a statement terminator.

Upvotes: 6

Sapiens
Sapiens

Reputation: 51

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

Upvotes: 0

Mikola
Mikola

Reputation: 9326

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

Upvotes: 6

Related Questions