C global variables

Introduction to C global variables

insideC variables and typesI explained how to use variables.

In this article, I want to mentionGlobal and local variables.

A kindLocal variableIt is defined inside the function and is only available inside the function.

like this:

#include <stdio.h>

int main(void) {
  char j = 0;
  j += 10;
  printf("%u", j); //10


A kindGlobal variableIs defined outside of any function, as shown below:

#include <stdio.h>

char i = 0;

int main(void) { i += 10; printf("%u", i); //10 }

Any function in the program can access global variables. Access is not limited to reading values: variables can be updated via any function.

Therefore, global variables are a way for us to share the same data between functions.

The main difference with local variables is that after the function ends, the memory allocated for the variables will be released.

Only release global variables at the end of the program.

Download mine for freeC manual

More clang tutorials: