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?
#include "stdio.h"
int *ptr;
int func(void)
{
/** abc is a local variable **/
int abc = 132;
ptr = &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;
}
Results: the value of abc
=200
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...".
Can I say the following about variables?
1) Scope attribute comes under compile time.
2) Lifetime attribute comes under run-time.
3) Visibility attribute comes under compile-time
Yes, C's memory model allows you to access anything easily so you can actually do things like the above and see "interesting" results.
However, what you did here is specified as undefined behavior (UB) by the C standard. That means literally anything can happen; that might be what you expect, or it might not.
Note that you did not access "the local variable" because by the time you make the access func
has already returned, so the lifetime of its local variables has expired. What you did access was a memory region that "just happened" to have an interesting value. If you called func1
from inside func
then the behavior would be well-defined.
A few more notes:
Scope is definitely a compile-time-only concept; the scope of a name (variable, identifier, etc) is the subset of the program code where that name is recognized by the compiler.
This is very different than the lifetime of variables, which is independent of scope in the general case, and conflating the two is a common mistake. The lifetime and scope of local variables are indeed intertwined, but that's not true of everything.
While in theory it's "just UB", in practice it is asking to actually fail. The location of abc
is (in every implementation I know of) somewhere on the stack. Since you've left the initial block and then entered some other block, it is quite likely that something else will occupy that location of memory. Which you are going to overwrite.
what is meant by scope?
The scope of a variable is the portion of the text in which the variable can be referenced. A local variable has block scope: it is visible from its point of declaration to the end of the enclosing function body.
It has nothing to do with lifetime of a variable. It is the storage duration which tells about lifetime of a variable.
Does it come into the picture at run time or compile time?
It comes into the picture at compile time and linking time. When the program will try to access a local variable outside of its block, compiler will give you an error about that undeclared variable(which is local to its block).
This example will explain it better:
#include <stdio.h>
void userlocal(void);
int main()
{
int a= 2;
printf("local a in outer scope of main is %d\n",a);
userlocal();
printf("local a in scope of userlocal is %d\n",b); // This will give error at compile time
return 0;
}
void userlocal(void)
{
int b = 20;
printf("local a in scope of userlocal is %d\n",b);
}
Output:
[Error] 'b' undeclared (first use in this function)
Can I say the following about variables?
Yes you can say.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With