Note that there are some explanatory texts on larger screens.

plurals
  1. POIs scope in C related only to compile time, as we know we can access any memory at run time?
    primarykey
    data
    text
    <p>I was trying to understand the exact meaning of scope in C. What I could understand is that scope is limited to compile time only. For example, in case you access the local variable from some other function. This will result in a compile time error. On the other hand, the following program works fine. This means that the C has a flat memory model and anything can be accessed at run time. C books associate scope with lifetime and variable visibility, I found it quite confusing. I think all these terms makes sense only for the compile time. Can someone please throw light on it?</p> <pre><code>#include "stdio.h" int *ptr; int func(void) { /** abc is a local variable **/ int abc = 132; ptr = &amp;abc; return 0; } int func1(void) { /** although scope of abc is over still I can change the value in the address of abc **/ *ptr = 200; printf("the value of abc=%d\r\n",*ptr); } int main(void) { func(); func1(); return 0; } </code></pre> <p>Results: the value of <code>abc</code>=200</p> <p>In the simpler words, what is meant by scope? Does it come into the picture at run time or compile time? As we can see, we can access anything at the run-time. But, if we don't follow the rules, then we will get the compilation error. For example, local variable reference in an another function. The compiler will throw an error saying, "variable not defined...".</p> <p>Can I say the following about variables?</p> <pre><code>1) Scope attribute comes under compile time. 2) Lifetime attribute comes under run-time. 3) Visibility attribute comes under compile-time </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload