Minor compilation error in Visual Studio 2008

I have a problem compiling the following code:

#include <stdio.h>
#include <limits.h>
int main () {
    printf("short: [%d,%d]\n",SHRT_MIN,SHRT_MAX);
    printf("int: [%d, %d]\n",INT_MIN, INT_MAX);
    printf("long: [%d, %d]\n",LONG_MIN,LONG_MAX);
    int aa=017;
    printf("%d\n",aa);
    return 0;
}

Error message:

1>c:\tic\ex1\ex2\ex2.c(12) : error C2143: syntax error : missing ';' before 'type'
1>c:\tic\ex1\ex2\ex2.c(13) : error C2065: 'aa' : undeclared identifier

However, compiling for this is fine:

    #include <stdio.h>
    #include <limits.h>
    int main () {
        int aa=017;
        printf("short: [%d,%d]\n",SHRT_MIN,SHRT_MAX);
        printf("int: [%d, %d]\n",INT_MIN, INT_MAX);
        printf("long: [%d, %d]\n",LONG_MIN,LONG_MAX);
        printf("%d\n",aa);
        return 0;
    }

Any idea what the problem is?

+3
source share
7 answers

In C, variables that were previously supposed to be declared at the top of the scope, before any code is executed. This does not apply to C99 (which Visual Studio does not implement.)

+10
source

Visual Studio does not support C99, so all declarations (for example, for aain your example) in the block should appear before any statements (for example, your calls printf).

+3
source

() C , , ++ C-. , .

+1

"" C (C89/90) . aa ++ "" C (C99), C89/90.

VS 2008 C89/90, .

, printf long %ld, %d.

+1

C C99 . , , GMan. , , , :

int main() { 
    int x;

    printf("whatever");
    int y; // not allowed

    { 
         int z;    // allowed
    }
    return 0;
}

( - , if while) , C.

+1

cpp, ( ++ )

0

, C, doesn; t visual studio .c ++ ? //commnets

0
source

Source: https://habr.com/ru/post/1728379/


All Articles