I understand why passing value in a function does not work as main function will not have access to changed value (change takes place in the stack frame of called function

However if I declare variable in global frame, will calling function have access to the changed variable in the called function?

I could not compile the following code as it gave error: declaration shadows a variable in the global scope [-Werror,-Wshadow].

how do I rectify the error to test if global variable declaration will work

#include <cs50.h>
#include <stdio.h>
int a = 10;
void increment (int a);
int main(void)

 increment (a);
 printf ("a = %d\n ", a);


void increment (int a)

1 Answer 1


Interesting question. The answer is that a global variable does not work in your program. The compiler warning is really telling you that the variable a, in main, is different from the variable a in the increment function. Again, the increment function is passed a local copy of a, and is only increased locally, but not globally as you intended. To check, you can compile with the following command, without using make:

cc program.c -o program

In the IDE command line, where program is the name of your program.

In general, it is not a good idea to use global variables, but we can do it if we are careful, for example, we should not rename these variables, or as in this case, not use them in the definition of parameters of a function. I hope that helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .