0

This may not be a very useful question, but I am curious.

The old C dialect in Visual Studio 2010 doesn't allow mixing declarations with executable statements, so this program gives an error:

int main(void) {
    int a;
    a = 1;
    int b;
    b = 2;
    return 0;
}

However, the error reported is this:

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

I'm at a loss which construction in its (obsolete) C grammar that the compiler thinks we are trying to use, and where a semicolon would help. I realize that it might just be a badly formulated error message, or an unintended effect of how the parser is written, but maybe there is something I'm missing.

EDIT:

Since several people have now answered this question with The old C dialect in Visual Studio 2010 doesn't allow mixing declarations with executable statements, or words to that effect, perhaps I didn't make myself very clear. Sorry about that. To try to clarify: Yes, I already know that. I'm just curious about the error message.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
  • 3
    That's just the error that the compiler spits out I guess. It is explicitly called out in the documentation (look at the final example): http://msdn.microsoft.com/en-us/library/0afb82ta.aspx The docs say: *The compiler expected a specific token (that is, a language element other than white space) and found another token instead.* Basically the compiler encountered a token that it cannot deal with. – David Heffernan Dec 16 '13 at 12:46
  • With Visual Studio 2013 this will become a moot point since they will support [Mixing declarations with code](http://msdn.microsoft.com/en-us/library/hh409293.aspx) so I suspect this was something they didn't feel the need to address specifically in old products and won't need to in new ones. – Mike Dec 16 '13 at 13:28
  • possible duplicate of [error C2275 : illegal use of this type as an expression](http://stackoverflow.com/questions/9903582/error-c2275-illegal-use-of-this-type-as-an-expression) – rubenvb Dec 16 '13 at 14:02
  • @rubenvb - How is that a duplicate? It doesn't answer the question of why Microsoft selected "missing ;" as the error message for a mixed declaration not being allowed. – Mike Dec 16 '13 at 14:08
  • @Mike the answer applies though. The error message is not at all important in this case, it's purely a failure on the compiler's side to actually tell you what's wrong. If that's what the question is about, it's a stupid question. – rubenvb Dec 16 '13 at 14:14
  • @rubenvb: The question _is_ about the error message. It may be a "stupid" question, even if I myself don't think that it is particularly stupid, and it probably is uninteresting to most people who don't have an interest in compiler implementation. But I don't think I can make the question any clearer than I have already done. – Thomas Padron-McCarthy Dec 16 '13 at 15:20
  • 1
    It is a *30 year old* C compiler. A big reason Microsoft won't update it is that nobody wants to touch it with a 10 foot pole. Trying to guess how its parser was implemented back then is neither productive nor interesting. – Hans Passant Dec 16 '13 at 15:59
  • 1
    @HansPassant: Of course, it's about as productive and useful as trying to read braille from tea-leaves. That doesn't mean it you shouldn't give it a whirl, if you feel like it... and seeing as I have a touch of OCD in me, I completely understand why the error msg is _"bothering"_ the OP. To quote an all time great writer: _"It is a very sad thing that nowadays there is so little useless information"_ – Elias Van Ootegem Dec 16 '13 at 16:13
  • 1
    @ThomasPadron-McCarthy: Your question was crystal-clear from the off, it's just that people read the title, see C2143 error + VS, see the code and don't read the question. It's a silly error msg, probably because the compiler tries to make sense of things, trying to interpret `int` as being part of a cast, function pointer or some other expression. When that fails, it's gone through all those options, and picks the most likely error message (missing parentheses, curly's, semicolon, all 3 checked, all 3 missing, likely assume that's the problem, especially since the line numbers match) – Elias Van Ootegem Dec 16 '13 at 16:21
  • I've found a way this error can be generated, under microsoft visual studio, if you have a class called say "Expression" and then you create a variable called "Expression", and then you try and create an instance to "Expression" in the same function, it just tells you that there's a semi colon missing, rather than any hint of a naming conflict. – Owl Sep 16 '16 at 08:43

1 Answers1

1

I think it's just down to the fact that the C2143 error just churns out rather generic error messages. Looking at the MSDN documentation
On this page it states that:

C2143 can occur when a closing brace, parenthesis, or semicolon is missing on the line where the error is detected or on one of the lines just above

Now because you're declaring an int after a non-declaration instruction, it could be that the compiler just grinds to a halt right after encountering int b. Perhaps the compiler checks to see if there's a closing brace (signaling the end of the function block), or if the int would be part of a cast of sorts.
Since that's clearly not the case, the compiler has done quite a few checks that have nothing to do with mixing declarations in with non-declaring stmts.

Another guess might be that this is a generic error message that is produced with any type of invalid declaration:

struct foo {
    int a;
    int b // missing ;
};//struct decl invalid

int a, char b;//invalid... obviously

int my_func( void ) // error
int another_f (int b);

All of these would, then, produce the same C2143 error, and, if I guessed correctly, it should then also produce the same message...

My 2 cents

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • So many things can generate this error message, and none of them have anything to do with semicolons. For example mistyping the class name when defining the member function can produce hundreds of these obtuse errors along with missing ")" errors. I am trying to work one out. It seems to be the default error Visual Studio raises. – Owl Sep 15 '16 at 15:40
  • 1
    It's not that it's the default error. Once you omit a token (like closing brackets or `;`), the parser can't really make sense of your code. A missing character is rarely (if ever) picked up on on the same line, the parser just carries on until it encounters a token that doesn't make sense (eg: `int i = 123 + 34\nconst char *foo = "bar";`). That's why you get these kinds of errors as often as you do: typo's almost always result in parser errors... – Elias Van Ootegem Sep 15 '16 at 16:25
  • +1 Oh I see, that kind of makes sense. G++ / GCC seems to be better at working out what the errors are than Visual Studio. The problem really is, if you have a semicolon error, you aren't necessarily able to work out what causes it, it could be a bunch of things. – Owl Sep 16 '16 at 08:14
  • @Owl: True. Don't know whether or not GCC is *better* at this kind of stuff... couldn't even comment on whether or not there is such a thing as *better* in this case: A compiler will never be able to make sense of invalid code (syntax errors), so regardless of what errors you get, it's technically impossible for a compiler to tell you exactly what you did wrong. If it could do that, you wouldn't have to write the code, or the syntax would be allowed – Elias Van Ootegem Sep 16 '16 at 09:25