Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C variable declaration place [duplicate]

Tags:

c

Possible Duplicate:
Variable declaration placement in C

I really dont understand why when I declare variable 'm' like in snipped code below why it doesn't work???I declare m before I use it so what's the point?? thanks

    int main(){

    int a[] = {2,-4,6,47,59,-6,0};
    sort(a, 7);

    int m;
    for(m = 0; m < 7; m++){
        printf("%d ",a[m]);
    }
}

But if I put declaration at beggining, above the array, it works.

like image 962
exeq Avatar asked Feb 18 '23 16:02

exeq


2 Answers

Looks like you are compiling in ANSI C mode. In C89, variable declaration is allowed only at the beginning of a block.

Since C99, this restriction has been removed. Compile with -std=c99 which will allow you to declare variables anywhere.

like image 153
P.P Avatar answered Feb 27 '23 01:02

P.P


as far as i know in C, all declarations must be above the code

like image 20
Abdusalam Ben Haj Avatar answered Feb 27 '23 01:02

Abdusalam Ben Haj