Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the semicolon in C++?

Tags:

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?

like image 946
Mihran Hovsepyan Avatar asked Jun 24 '11 06:06

Mihran Hovsepyan


People also ask

What is semicolon called in C?

In a C program, the semicolon is a statement terminator.

Do you need a semicolon in C?

The semicolon appears at the end of every line of code in C, except for after certain braces (e.g. if statements, function definitions). So a semicolon is still needed after a forward declaration.


2 Answers

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

like image 79
fredoverflow Avatar answered Sep 22 '22 00:09

fredoverflow


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.

like image 23
Hyperboreus Avatar answered Sep 22 '22 00:09

Hyperboreus