scope - C Local variable has same name as function - how does it work? -
i teach c absolute beginners , have noticed of students notion use same name function , local variable in function. think it's goofy , prevent recursion.
here's example:
int add2numbers (int a, int b) { /* tested on mac os x gcc */ int add2numbers = + b; return add2numbers; }
the way understand how works variable in local scope of function, , function in global scope.
so, questions...
- am understanding correctly?
- where h*** getting idea from?
thanks
you correct in assuming function global , variable local. reason why there no conflict in program.
now consider program given below,
#include<stdio.h> int x=10; void x() { printf("\n%d",x); } int main() { x(); return 0; }
you error because in program both function x()
, variable x
global.