| Global variables retain value?

Previous | Memory, C Variables, and Pointers | Next

Q:

If I understand correctly, if I have the following global variable in my program

    int a = 10;

and if I change its value somewhere in the program to 20 (for example), its initial value will be 20 (not 10) when I run the program next time???

A:

Exactly! Note that this is true only for global and static variables. To force reinitializing, you must put explicitely something like

    a = 10;

at the begining of the main program!
Note, however, that if the program is archived, the initial values will be restored each time you run the program, because archived programs are reloaded from the archive memory to the RAM on each start, similarly to the programs are reloaded from disks on "standard" computers (PC, etc.) each time when you start them. The same is true for compressed programs (for obvious reasons) and if you use a data variable and have TIGCC create a copy every time it is used.

See also: How do I store variables so they retain their values