+ 4
Question regarding extern in C
A) #include<stdio.h> int a = 50; extern int a; void fun() { int a = 30; printf("%d",a); } int main() { fun(); } B) #include<stdio.h> int a = 50; void fun() { extern int a; int a = 30; printf("%d",a); } int main() { fun(); } Option A doesn't give any error. But option B does give an error. However both have int a = 30 as local variable. Can anyone explain why?
1 Resposta
+ 1
I don't know what exactly is causing the error, but why are you trying to define the variable again? The variable is already defined as an integer and you're trying to define it again.
This might work:
#include <stdio.h>
int a = 50;
void fun(){
extern int a;
a = 30;
printf("%d",a);
}
int main(){
fun();
}