Implementation of static in c | Sololearn: Learn to code for FREE!

+4

Implementation of static in c

We say static variable/functions are visible only in the file ,in which these are declared. How it is implemented internaly?

1/12/2019 4:38:49 AM

punnoose

15 Answers

New Answer

+4

punnoose Variable have something called "linkage specification". There are two types 1. external linkage - means visible everywhere like global variables have 2. internal linkage - visible within a translation unit only. static variable have internal linkage, hence they are not visible outside a translation unit. Due to this you can hava a same variable name of same type in two different C files. static variables are stored in .Data Segment if initialised with value other than 0 0 initialized or default initialized static variables goes into .BSS segment (Block Started by symbol) There ia no such thing as non initalized static variables. They are always initialized to their default values if not explicitly initialized static int i; // goes in .BSS segment, default initialized to 0 static int i=10; // goes into .Data seg with value 10

+4

Rewa Mathur In C it can be difficult to avoid global variables Concurrancy issues - if you are changing the state (i.e. modifying the variables, else global const or enums are fine in C) Memory issues? like? (failure to dellocate is programming error. Memory wise it can be benificial like you can have bigger arrays in global scope than in local scope (old knowledge may/may not be relevant with today's compilers) Testing issue - sure you can have namespace pollution - that's why you have static :)

+3

Bennett Post By "static external" i guess you mean variables and functions declared in the header file. They are still a part of the same translation unit. There is no such thing as "static external"

+3

Rewa Mathur technically "uninitialized" static or global variables is still a wrong terminology.

+2

Rewa Mathur i do love the way you explain, thanks for that

+2

Bennett Post Ok :) Global static is a better word though :)

+2

Rewa Mathur Concurrency issue arise only if you are changing the state of the variable, otherwise it doesn't matter how many thread access it. Not sure what you are saying about coupling, an example would be helpful variable search goes from inner most scope i.e local to outermost scope i.e global. Hence local variables are more tightly coupled than anything else. Local variable with same name hides global variable.