Ferenc Deak
Ferenc Deak

Reputation: 35408

Compiling C code with MSVS2012 and two ";;"s

Code:

char* to_return = NULL;;
char* final_encoding = NULL;

Fails to compile.

Error: error C2143: syntax error : missing ';' before 'type'

Same behaviour for:

int something = 0;;

(I just wanted to be sure that the NULL macro is not messing things up)

Code:

char* to_return = NULL;
char* final_encoding = NULL;

Succeeds.

Compiled as C not C++... with Visual Studio 2012. Why???

Edit:

The following code triggers this erroneous behaviour:

void test_fun()
{
    ;
    int a = 0;
    if(a == 1)
    {
            return;
    }
}

Upvotes: 2

Views: 86

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263197

char* to_return = NULL;;
char* final_encoding = NULL;

Whether this is legal depends on where it appears (inside or outside a function definition) and on the version of the language that your compiler supports.

A lone semicolon is a null statement. I don't think there are any cases where a null statement is necessary, but it can improve clarity, as in:

while (*foo++ = *bar++)
    ;

If the above declarations appear at file scope, outside any function definition, then the extra semicolon is a syntax error; there is no such thing as an empty declaration, and statements are not allowed at file scope.

If they appear at block scope, somewhere between the { and } of a function definition, then they're valid if your compiler supports C99 or later. If your compiler only supports C90 (as I believe Microsoft's compiler does), it's a syntax error. (C++ has always permitted mixed declarations and statements.)

On the other hand, some compilers may permit a stray semicolon at block scope as a language extension. gcc does so by default; with the -pedantic option, it issues a warning.

In C99, you have a declaration, followed by an empty statement, followed by another declaration, which is perfectly legal at block scope.

In C90, declarations may not follow statements within the same scope, so again it's a syntax error.

In any case, the solution is easy: delete the extra semicolon.

Upvotes: 0

Utkan Gezer
Utkan Gezer

Reputation: 3069

Don't take my word on this, but it likely is due to having a statement before a variable declaration inside a code block; where the statement is ;, the empty statement, and the varaible is char* final_encoding.

My Microsoft Visual Studio 2013 doesn't complain, and as far as I know VS 2012 doesn't support C99 not even as much as VS 2013 does, although 2013 also doesn't fully comply either.

Upvotes: 4

Related Questions