c - Redefined Global variable -


i'm little confused code result:

#include <stdio.h> int g; void afunc(int x) {      g = x; /* sets global whatever x */ }  int main(void) {     int g = 10;    /* local g 10 */     afunc(20); /* function set 20 */     printf("%d\n", g); /* print "20" */      return 0; } 

why result 10 not 20 ?

calling afunc changes global g, , main retains local g.

entering function doesn’t swap scope global scope. each function* has own scope.

* among other things


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -