extern and global in c

Posted by JPro on Stack Overflow See other posts from Stack Overflow or by JPro
Published on 2010-04-16T11:36:32Z Indexed on 2010/04/16 11:43 UTC
Read the original article Hit count: 340

Filed under:
|
|

Can anyone please tell me is there any special requirement to use either EXTERN or GLOBAL variables in a C program? I do not see any difference in a program like below, if I change from gloabl to extern.

#include <stdio.h>
#include <stdlib.h>
int myGlobalvar = 10;

int main(int argc, char *argv[])
{
  int myFunc(int);
  int i;
  i = 12;
  myGlobalvar = 100;
  printf("Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  i = myFunc(10);
  printf("Value of passed value : %d\n",i);
  printf("again Value of myGlobalvar is %d ,  i = %d\n", myGlobalvar, i);
  system("PAUSE");  
  return 0;
}
int myFunc(int i)
{
    i = 20 + 1000;
//extern int myGlobalvar;
myGlobalvar = 20000;
//    printf("Value of passed value : %d",i);
return i;
}

If uncomment extern int myGlobalvar, the value does not change.

Is there any correct difference between both?

Can anyone please correct me?

© Stack Overflow or respective owner

Related posts about c

    Related posts about variables