Ador Dastan
Ador Dastan

Reputation: 85

Does it change anything to write one semicolon or more at the end of a line in C++

like in this case:

void function(){
...
uint8_t type = some_func(arg);;
...
}

I know semicolons are end of statements in c++ but writing multiple semicolons will it affect anything like maybe a tiny bit of processing time?

Upvotes: 3

Views: 363

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

Such a semicolon introduces a null-statement that has no effect.

You can use a null statement for example in a loop when the body of a loop is empty.

Here is a loop to set a pointer to point to the terminating zero of a string.

char *p = s;
for ( ; *p; ++p );

In C you need to use a null statement after a label when the label precedes a declaration because in C labels may not be placed before declarations.

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    const unsigned int N = 10;
    unsigned int i = 1;

    L:; 
    unsigned long long int square = i * i;

    printf( "%u\t%2llu\n", i, square );

    if ( ++i < N ) goto L;

    return 0;
}

Pay attention to the construction

L:; 

Very often beginners make an error when they by mistake place a semicolon after a loop. For example

#include <iostream>
#include <cctype>

int main() 
{
    const char * s = "hello";
    const char *p = s;

    for ( ; *p; ++p );
    {
        std::cout << char( std::toupper( ( unsigned char )*p ) );
    }
    std::cout << '\n';

    return 0;
}

Instead of outputting the sequence of characters "HELLO" the program outputs nothing.

*Similarly to the semicolon some C++ constructions accept a redundant comma. For example in a braced list of initializers.

int a[] = { 1, 2, 3, };
                  ^^

However you may not place such a comma in the parameter declaration list

void f( int x1, int x2, int x3, );
                            ^^^^ compilation error

Upvotes: 3

user31264
user31264

Reputation: 6737

Semicolon after another semicolon (or after if (...), after for(...), and so on) introduces a statement which does nothing. It can be harmful, e.g. in the following:

if (x > 0)
  std::cout << "x is positive";;
else
  std::cout << "x is negative or zero";

Which is the same as

if (x > 0)
  std::cout << "x is positive";
;
else
  std::cout << "x is negative or zero";

The second semicolon ends if-block, making the following else "an orphan" (else without if), causing compile error.

You should avoid extra semicolons.

Upvotes: 0

It can affect things. For instance, this if statement is ill-formed

if(foo())
  bar();;
else
  baz();

The extra semi-colon introduces an empty statement. The statement does nothing, but it breaks the association of the else to the if. Naturally, it's common wisdom to make judicious use of curly braces ({}) for compound statements for such reasons (and so this will likely only rarely occur in good code base). But since you did ask, that's one way the extra sem-colon can affect a parse.

As for processing time, assuming the program is well-formed. You will see no effect from an empty statement.

Upvotes: 8

Related Questions