What is the use of static variables? I cannot understand the uses of it clearly. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the use of static variables? I cannot understand the uses of it clearly.

#include <stdio.h> void say_hello(); int main() { int i; for (i = 0; i < 5; i++) { say_hello(); } return 0; } void say_hello() { static int num_calls = 1; printf("Hello number %d\n", num_calls); num_calls++; }

16th Dec 2019, 12:53 PM
SAzidsukc
SAzidsukc - avatar
2 Answers
+ 4
Static variable value will be initialized only ones in total program, and can shared same value throughout the program with current changes... Observe the behavior of num_calls values in your program with static, with out static... You will find the difference..
16th Dec 2019, 1:09 PM
Jayakrishna 🇮🇳
+ 3
Static variable retain it's value during function calls. So making a call to that function will result in updated values of the static variable.
16th Dec 2019, 2:02 PM
Avinesh
Avinesh - avatar