What are the differences between normal variable and static variable in function concept? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

What are the differences between normal variable and static variable in function concept?

2nd Jul 2019, 9:58 AM
Rajnish Kush
Rajnish Kush - avatar
4 Answers
+ 1
I just try to give you an example. void foo(int y) { int x = y; } When a block of program ie function ends the memory is automatically deallocated by compiler & that's why the variable x has no existence in our program. But for static variable, the memory is not deallocated until the entire program ie main() ends.
30th Aug 2020, 6:14 PM
Bibhash Ghosh
Bibhash Ghosh - avatar
+ 5
Normal variables are destroyed at the end of the function, because they are allocated on the stack. Static variables aren't. They always exists, even after function call.
2nd Jul 2019, 10:13 AM
Théophile
Théophile - avatar
+ 1
Bibhash Ghosh It turned out that the question was posted a year ago. I perfectly understood the concept. Thanks sir.
30th Aug 2020, 6:37 PM
Rajnish Kush
Rajnish Kush - avatar
0
Yeah I have noticed that after committing. Glad it helps.
30th Aug 2020, 6:49 PM
Bibhash Ghosh
Bibhash Ghosh - avatar