Range of variables in C

Understand what scope is and how it works in C

When you areCProcedure, depending on where you declaredrange.

This means it will be available in some places, but not in others.

The location determines 2 types of variables:

  • Global variable
  • Local variable

The difference is that the variables declared inside the function are local variables, as shown below:

int main(void) {
  int age = 37;
}

Local variables can only be accessed from inside the function, and they will cease to exist at the end of the function. They are cleared from memory (with some exceptions).

The variables defined outside the function are global variables, such as the following example:

int age = 37;

int main(void) { /* … */ }

Global variables can be accessed from any function of the program and are available before the entire program is executed until the end.

I mentioned that after the function ends, local variables are no longer available.

The reason is that local variables are inheap, By default, unless you explicitly allocate them on the heap usingpointer, But you must manage the memory yourself.

Download mine for freeC manual


More clang tutorials: