[ACCEPTED]-Effect of return type being static-c

Accepted answer
Score: 39

The function is static, not the return type. This 6 means that its name is only visible from 5 within the current compilation unit, which 4 is used as an encapsulation mechanism.

The 3 function can still be called from elsewhere 2 through a function pointer, however.

See 1 also this discussion on the general static keyword for more context.

Score: 1

We use static data type when returning a 9 pointer to a variable created in called 8 function.for e.g

float * calculate_area(float r) 
{
    float *p;
    static float area;   
    p=&area;   
    area=3.14*r*r;
    return p;
}

If you would make area as 7 automatic variable i.e without any type 6 qualifier it would be destroyed when control 5 returns from called function.When declared 4 as static you could correctly retrieved 3 the value of area from main also.Thus it 2 in order for it to persists its value we 1 make it as static.

Score: 0

By using static , Access to static functions is 3 restricted to the file where they are declared Another 2 reason for making functions static is to 1 reuse the same function name in other files.

//file1
static void fun1(void) 
{ 
  cout<<"file1";
} 


// file2
// now if we include file1 and use fun1 in file2 , it
// will show “undefined reference to `fun1’” error as 
// it fun1 is defined static

int main(void) 
{ 
  fun1();  
  getchar(); 
  return 0;   
} 

More Related questions