Suppose that we have declared a global variable say int x in C language. A variable with the same name i.e., int x is also declared in a function so that it is a local variable to that function. Now, the question is how to access global value of x in that function?
Local Vs Global variabls
Collapse
X
-
Tags: None
-
-
Hello Vidya
In C, the local variable takes precedance over the global, unless the global is declared static, in which case the local one cannot even be declared.
Regds,
BhushanComment
-
No declaring a global static does not prevent you declaring a local variable with the same name, for example this is valid
Output:Code:#include <stdio.h> static void PrintGlobal(void); static int V1 = 5; int main() { int V1 = 10; printf("V1: %d\n", V1); PrintGlobal(); return 0; } static void PrintGlobal(void) { puts("Global Data"); printf("V1: %d\n", V1); }
V1: 10
Global Data
V1: 5Comment
Comment