SOLVED - Entire life time of the program

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
I don't understand what is meaning of "Entire life of the program" with respect to variable and function declaration in c programming.

A project can have multiple source files like main.c, File1.c, File2.c, File3.c etc. We can access variables declared in another file in main.c file. We can call the any type of function from any other sources file in main.c by using extern storage class

if the global variable declare in main.c it can only accessble for this source file only.

if the local variable declare in side function in main.c it can only asscessble for the function only

What is "Entire life of the program"?
 

Dave Lowther

Joined Sep 8, 2016
332
if the global variable declare in main.c it can only accessble for this source file only.
That is not correct. The variable can be accessed from other files if it is declared as extern in the other files.
If the global variable is declared static in main.c then it can't be accessed from other files.
What is "Entire life of the program"?
From some time before main() is entered until the code returns from main()
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
From some time before main() is entered until the code returns from main()
i don't understand anything about "Entire life of the program" from your description.

Can you please explain what does this mean for you?

i understand, we can access functions and variables declared in other source files with the help of extern storage class
 

Dave Lowther

Joined Sep 8, 2016
332
i don't understand anything about "Entire life of the program" from your description.

Can you please explain what does this mean for you?
I'll try :)
Local variables declared inside functions exist only for the lifetime of the function, i.e. a variable declared at the beginning of a function, without the 'static' attribute, is created on entry to the function and destroyed on exit from the function.
Global variables always exist from the point of view of any of your code. They are created before any of your code runs and destroyed after your code finishes running. If your code runs forever then the global variable exists forever.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Global variables always exist from the point of view of any of your code. They are created before any of your code runs and destroyed after your code finishes running. If your code runs forever then the global variable exists forever.
It seems to me that the lifetime of the program means the lifetime of the global variable. life time of global variable is entire runtime of program. almost got it.
 

Dave Lowther

Joined Sep 8, 2016
332
It seems to me that the lifetime of the program means the lifetime of the global variable.
That's kind of correct, although I think of it the other way around (the life time of the program causes the lifetime of the global variable and not vice versa). You seem to have understood it well enough.
 
Top