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...

  1. am understanding correctly?
  2. 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.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -