Search code examples
cexternstorage-class-specifier

extern storage class output


somy question is, **Is there two variable named x in the main ,one goes to g() with value 1 go there prints 2 and another one keeps at 1 again prints 2 in main. **

#include <stdio.h>
void f(){
    extern int x;
    
    x++;
    printf("%d",x);
    
}
  int x;
void g(){
   
    ++x;
    printf("%d",x);
}
int main() {
    // Write C code here
    x++;
    g();
    
    printf("%d",x);

    return 0;
}

Output : 22


Solution

  • Here x in main and the one defined outside g() in global scope are referring to the same x. Also the variable x is extern through out the code because of the previous declaration.

    Try printing the address of x in the required location.

    Refer this answer for more info