What is difference between static and global variable in C? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is difference between static and global variable in C?

Since both static and global variables can be used anywhere in whole code ,then what really is the thing differentiating them?

15th Sep 2021, 2:48 PM
Muhammad Bilal
Muhammad Bilal - avatar
5 Answers
+ 2
On Sololearn you won't observe a difference. In real development the difference arises when you are working with multiple source code files. Use static to declare that a variable is only global to the source file it is contained in. A static global won't be recognized outside of its source file, wheres an unqualified global can be visible in the entire project. In that case you would use the extern keyword to declare that is defined elsewhere. https://stackoverflow.com/questions/7837190/c-c-global-vs-static-global#7837369
15th Sep 2021, 3:24 PM
Brian
Brian - avatar
+ 2
Arsenic eghh ... i was talking about the const variables .. ma bad
15th Sep 2021, 5:54 PM
ox titanium
ox titanium - avatar
+ 1
‎ ‎M.A.G can you provide an example where changing global variable caused compiler error ?
15th Sep 2021, 4:45 PM
Arsenic
Arsenic - avatar
+ 1
They're both stored in the data segment; the difference is that the global has an externally-visible linker symbol, and the static global does not.
17th Sep 2021, 4:28 AM
Arun Jamson
Arun Jamson - avatar
0
so global applies to the whole program and is always active, static is locked in a function and has to be called separately where is needed?
30th Sep 2021, 3:12 PM
magic real
magic real - avatar