When programming in Visual Studio, if you happen to run across this error while trying to compile C code, one thing to check on is whether or not you're declaring your local variables inline. For whatever reason, Visual Studio hates this and issues a syntax error for the next line of code. One that's of no use whatsoever in helping to figure out what to do.
So, for example, if you have a function like:
int foo(int x)
{
assert(x != 0);
int y = 4/x;
return y;
}
You would need to rewrite your code like this:
int foo(int x)
{
int y;
assert(x != 0);
y = x/4;
return y;
}
Strange but true. I suspect there's an option somewhere to turn off the strictness of the compiler, but I haven't found it.
Hello,
I found an other cause on http://rt.cpan.org/Public/Bug/Display.html?id=12925
"microsoft C compiler wants all variables declared at top of function", which is what is being done in the second example.
That worked perfect for me.
Posted by: Johann | March 29, 2006 at 05:39 AM
It's no surprise that Microsoft's compiler wants the variables at the beginnig of the function: Standard C wants them there.
Atleast, this is what C89 defines. C99 allows variable declarations also in the middle of a function.
Posted by: Martin v. Löwis | April 08, 2006 at 10:25 AM
I don't think is necessarily at the beginning of the function, I think the variables have to be at the beginning of the scope you are in, like this
int function ( )
{
int x = 1;
int y = 0;
if( x )
{
int z = 0; //this is ok
}
}
Posted by: John | February 04, 2009 at 12:31 PM