How we can access auto and static variables outside their scope in C?

difference between static local and static global variable in c
static variable in c
storage classes in c
extern variable in c
where will the space be allocated for an automatic storage class variable?
internal and external static variable in c
local static variable in c
static function in c

Auto and static variable have scope limited to the block in which they are defined. Since Auto variables are defined in the stack, if the function exits, stack is destroyed and memory for the auto variable is released. But I read somewhere "However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables reside." Is this correct?

Also, static variables are defined in the data section so it retains its existence till end of program. The scope is within the block in which it is defined. Is there any way through which we can access static variable from any other function? Also, Is there any way we can access static variable from any other file?

As you said, static variables exist through out the life cycle of the program i.e memory allocated to them is not destroyed as long as the program is running. So, to access such a variable out side its scope, we can pass around the pointer to that memory location via pointer. A small example to show the same

#include <stdio.h>
#include <stdlib.h>

int* func()
{
        static int a = 0;
        a++;
        printf("a in func = %d\n", a);
        return &a;
}

int main()
{
        int *p;
        p = func();
        printf("a in main from ptr : %d\n", *p);
        *p++;
        p = func();
        return 0;
}

As you can see in the example, func() returns the pointer to the static variable it has declared, and any one who wishes to access the variable a, can use that pointer. NOTE: we can only do this because static variable's life is through out the program. Now irrespective of the static variable being in a different function or a different file, as long as you can some how get hold of the pointer to that static variable, you can use it.

Now coming to the case of auto variable.

What happens if you run the above program changing a from static to auto? you will see that while compiling a warning warning: function returns address of local variable [-Wreturn-local-addr] is thrown and when executing, we get a segmentation fault. What causes this is that the auto variable exists only in its scope, i.e as long as the function func() is being executed, the variable a has memory allocated for itself. As soon as the function exits, the memory allocated for variable a is freed and so the value pointed to by pointer p is at some unallocated memory location (resulting in segmentation fault).

Internal static variable vs. External static variable with Examples in C , If you try to use these variables outside the function in which they are defined, you will get an error. Another important point is that variables a and� Global Variables # The variables declared outside any function are called global variables. They are not limited to any function. Any function can access and modify global variables. Global variables are automatically initialized to 0 at the time of declaration. Global variables are generally written before main() function.

Here's a very simple example:

void print_msg(const char* msg) {
  printf("The message is: %s\n", msg);
}

int main(void) {
  char m[] = "Hello, world!";
  print_msg(m);
}

Here, m is an automatic variable, which is not in scope in print_msg. But print_msg clearly has access to its value.

Don't confuse "scope" with "lifetime". The scope of a variable is that part of the program where the variable's name is visible (and thus can be used). The lifetime of a value is the period during program execution in which a value exists. Scope is about program text; it relates to compilation. Lifetime is about program execution.

Local, Global and Static variables in C, It tells from what part of code we can access a variable. A storage class is used to describe the following things: The variable scope. The location where the� These variables have local scope to that block only that means these can be accessed in which variable declared. Keyword 'auto' may be used to declare automatic variable but we can declare these variable without using 'auto' keywords. Consider the following declarations. int main() { auto int a; int b; . return 0; } Here, both variables a

Note, as comments rightly point out, I am making an assumption here, the assumption that the simplest case of calling another function is not what the question is about. This assumption was not (yet) confirmend or rejected by OP. This case is discussed e.g. in the answer by rici.

The existence of auto variables is not only exist to "within" their scope (simplified: only code between the same enclosing {} can use their identifier), they are also restricted to "during" their "chronological scope" i.e. their lifetime (simplified after starting the execution of the code in the function and finishing its execution). It is possible to access the memory location of a variable via a pointer, which was set to their address (which is only possible within their scope, because accessing via their identifier is necessary) as long as it is done during their lifetime, yes.

But how would that pointer be found from anywhere else? Maybe by being written (from inside their scope and during their lifetime) to a global variable.

But which "other" code should then use that value? (remember I am putting the call of functions at the side here) This requires multithreading/multitasking/multiwhatevering. Lets say there is an interrupt service routine doing it. It would have to see the same address space as the variables scope, i.e. no memory management units getting in the way with some virtual memory magic. This is not true for many multiwhatevering implementations, but admittedly for a few of them, so lets continue.

This imagined ISR would have to ensure that it only accesses the auto variable while it actually exists (i.e. during its lifetime), otherwise it would pretty much access what is effectively a meaningless random memory location. And this assumes that the ISR is actually allowed/able to access that memory. Even without MMUs, there are implementations which can/will have execeptions. This introduces the need for synchronisation mechanisms, e.g. semaphores.

So in certain environments it would be possible, but completley pointless (global variables are still involved), expensive, hard to understand and next to impossible to port. (remember I am putting call of a function aside here)

Similar for static variables.

In the case of function local static variables, they would at least reliably exist, but accessing them would still need the pointer value to be somehow transported out of their scope. For static variables that could actually be done via the return value of the function as demonstrated in the answer by yashC.

In the case of "static" variables understood as file scope restricted variables, the pointer still would have to be transported out of the file scope. This would merely defeat what is probably the point of a file scope restricted variable. But I could imagine some kind of access privilege scheme, as in "Here is the key to the vault. Handle with care."

As mentioned at the start of this answer, I am putting the call of other functions aside. Yes, the easiest way to leave the scope of a function is to call another one. If that other function has a pointer parameter, it can use it to read-access and write-access the auto variable of the calling function. That is the normal case of call-by-reference parameters as supported by C. Calling a function also provides another, even simpler way of read-accessing the value of an auto variable of the calling function, though not write-accessing and not actually accessing the autovariable itself, only using its value. That way is the trivial mechanism of a call-by-value parameter, it does not even require a pointer. Both ways (call-by-reference parameter and call-by-value parameter) conveniently guarantee that the value does not change during the execution of the called function. (this time I am putting the multi-threaded case aside, because that is discussed in the main part of this answer).

Storage Classes in C: auto, extern, static, register with Example, To understand storage classes and scope in detail, first we need to know more memory and real physical memory – well beyond the scope of this class. The bss section, like the data section, is for storing global and static variables. and can only access other function's automatic variables through the use of pointers. Following are some interesting facts about static variables in C. 1) A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. For example, we can use static int to count a number of times a function is called, but an auto variable

5.5. Variables in Memory — Applications in C for Engineering , A local variable is one that occurs within a specific scope. The keyword auto can be used to explicitly create these variables, but isn't A global variable is a variable that is defined outside all functions and available to all functions. A global static variable is one that can only be accessed in the file where it is created. you cannot declare variable static and access it from another module. static has 3 usages: 1) if variable declared static whithin block (eg. function) it will maintial it's value during invocations. int add(void) { static int var = 0; var++; return var; } int main(void) { printf("Var = %d", add()); // Here var = 1 printf("Var = %d", add()); // Here var = 2 return 0; }

Global Variables, extern, static, const, Learn storage classes of variables in C. Learn to use auto, extern, staic, register in C. It tells the compiler about where to store the variable, its initial value, scope If a static variable is declared outside all the functions in a program, it can be� Static variable’s scope is also local to that function in which they are declared i.e. static variable can be accessible within the same block. Automatic variable’s life time is local (limited), automatic variables exit till the function execution time, when program’s execution leaves the function execution, variables are destroyed.

Learn storage classes of variables in C : auto, extern, staic, register, The variable d has local scope and no linkage - it's no use outside of f(). By saying that c is static, we are saying that we want to allocate it once, and only apply to dynamic memory, then do apply to automatic and static pointer variables � Static variables in C have the scopes: 1. Static global variables declared at the top level of the C source file have the scope that they can not be visible external to the source file. The scope is limited to that file. 2. Static local variables declared within a function or a block, also known as local static variables, have the scope that

Comments
  • can you give an example of an auto variable that you'd like to access outside the function it was defined in?
  • And what do you need this for? There might be a better way to achieve whatever you are trying to do
  • Scope and lifetime are different things. You can use pointers to access objects outside their scope, but not outside their lifetime.
  • I just was made aware that I assumed that you are not interested in the case of variables being accessed from functions which are called from within scope and during life-time. Can you clarify?
  • @Akib: You accepted a bad answer that does not discuss how the address of an object may be passed to other functions during its lifetime. rici’s answer is better.
  • About the segfault thing: you wish! On pretty much any implementation the memory for the stack is actually always allocated as a single big chunk (barring automatic expansion through guard pages), and stack allocation/deallocation is just moving the top-of-the-stack pointer. For this reason, even if you read/write above the currently top of the stack, no access violation is triggered - you've simply written in a logically non allocated, but "physically" valid location, so either you are overwriting some unrelated local variable, or you are just writing on unused data.
  • Then again, if your implementation writes canary values on unused portions of the stack or is performing extra checks at each memory access (think AddressSanitizer builds) it may detect the problem; but in an optimized build, generally use-outside-of-scope is either silent or results in silent corruption.
  • If you run it under gdb you'll see it's dereferencing a NULL pointer. That's just a favor gcc does to you - it detects the problem at compile time (indeed, it prints a warning about it), and essentially transforms it to a return NULL to aid debugging (it's allowed to change whatever because you are dereferencing an invalid pointer, which is undefined behavior, so anything is allowed). Try to pass the address of the local out of the function through more convoluted means (e.g. a global volatile value) and you'll see that it all appears to work flawlessly.
  • @MatteoItalia +1 TIL
  • @yashC: The question does not ask about accessing an object after its lifetime ends. In this answer, it is shown that an object with automatic storage duration should not be accessed, as by the example program, after its lifetime. But that is not what the question asks about. Instead of showing an example about a situation that was not asked about for a behavior that one cannot properly use, the answer should show an example that was asked about that one can properly use. The fact is that it is perfectly proper to access an automatically allocated object by address during its lifetime…
  • It doesn't require multithreading; there is a much simpler case. You only need to pass the address of a local variable to a called function. It could be any function which takes a C-string (or an array for that matter). Or a function which returns extra values through call-by-reference.
  • @rici I would consider code being called from within scope to be within scope, at least for the idea of this quesiton. At least I think that the question is meant like that. Good input though. I will try to have my assumption clarified.
  • That is not what scope means. Scope is not the same as lifetime.
  • @rici Yes I know and it is exactly what I elaborate in this answer.
  • Re "The existence of auto variables is not only exist to "within" their scope": This is incorrect; the existence of objects with automatic storage duration is not limited to within their scope. Consider an object O, its identifier I, and code C in which I is not in scope. It is possible for O to exist and be accessed by C. This is demonstrated in rici’s answer.