Aviral Rana
Aviral Rana

Reputation: 21

Why does NULL assignment outside main function return an error

This piece of code return an error.

#include<stdio.h>
#include<stdlib.h>

int* a;
a = NULL;

int main(){
    printf("works");
    return 0;
}

whereas this one does not...

#include<stdio.h>
#include<stdlib.h>

int* a= NULL;

int main(){

    printf("works");
    return 0;
}

What's the difference and why does this show the re-declaration error? If I do the same thing inside main function it works. But not outside.

Upvotes: 0

Views: 62

Answers (1)

bruno
bruno

Reputation: 32586

a = NULL; is a statement you can put only in the body of function

int* a= NULL; is the definition with initialization of the global variable a

Upvotes: 1

Related Questions