whyn0t whyn0t - 1 year ago 49
C Question

MsVc++ weird error: not declared variable

If you can answer or have a suggestion it's thank you, but if you can't help don't put a minus and leave, this is just mean way and not helpful to any one

compiler is rising this error

1>....\server\sv_init.c(528): error C2143: syntax error : missing ';' before 'type'
1>....\server\sv_init.c(529): error C2065: 'v' : undeclared identifier

...(all instruction lines containing v)

here is a portion of the code :

while(shl>=7) {
int v = (sh>>shl)&127;// <-- Erro is here
if (v==0 || v=='"' || v=='%' || v=='@') {
tmp[ol++] = '@';
// Com_Printf("OUT:%02X\n",tmp[ol-1]);
if (ol==sizeof(tmp)-1) {
return 1;
tmp[ol++] = v+1;
} else {
tmp[ol++] = v;

I don't really know if it is a bug in Ms Visual Studio 2010 compiler, I tried to remove the line before the error line the code build fine, any help or suggestion would be thankfully welcomed

Answer Source

Your code is being compiled in C mode (since the file has a .c extension). In C mode you can't declare variables after statements. You have three options:

  • Change the code to declare variables at the start of the enclosing scope like this:
while(shl>=7) {
    int v; // declaration
    v = (sh>>shl)&127;

This may be a lot of work if there is a lot of code like this.

  • Compile in C++ mode by changing the file extension to .cpp or specifying the /TP option. This may work because C++ is mostly a superset of C, but there a few C constructs that C++ doesn't allow.
  • Use a different compiler that does support this feature in C, such as GCC.