Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
No googling. It has three applications, and all of them have the point of scope limitation.
1) static functions. Limits visibility of a function to file scope, helping in maintainability and compiler optimisation (inlining), but the latter point is not that important these days because of link time optimisation (LTO). Also allows including them in header files with inline attribute so that you don't have to use function-like macros and thus don't run into ++/-- side effect problems with the arguments.
2) file static variables. Like global variables, but limited to file scope. Makes clear that the variable isn't accessed throughout the rest of the code base.
3) function static variables. Like the previous point, but with function limited scope. Can be used for making a function remember something across calls, or to save stack space at the expense of the BSS segment. Is dangerous with multi-threading if the function might be used from different threads. -
Also, it's a relevant question in low level programming. If you can't answer that, it's as bad as not being clear about what "const" means exactly (it doesn't mean "constant"), or the merits and limitations of "volatile".
-
@YADU My compliment - TIL something new about C that I havn't used in decades, and that's pretty rare.
I mean, I have never used VLAs, either, because I think they're a bad idea, but at least I know that they exist. -
@Fast-Nop
It is not file scope, C does not have a file scope, static function and static variables outside of functions are limited to a single translation unit.
A translation unit can have, and often has, multiple files, every #include adds an additional file to the current translation unit.
In theory you can use a single file for multiple translation units, with preprocessor tricks and different command line options, but i newer saw this and it would be a horrible way to program. -
@happygimp0 That's what I meant, the file that the compiler actually sees (hence static functions in header files). Translation unit would have been precise.
I did see that approach in the old days, i.e. including all .c files in a master .c file to enable more compiler optimisations, in particular with global inlining - LTO was not yet a thing back then. One example would be the famous chess engine "Crafty".
Related Rants
-
DirtEffect7Guy: *hands me sheet of paper* What does this code do? Me: *looks through code written on the paper* Well, mo...
-
Python8”Are you planning on having kids in the near future?” Literally (very) illegal to ask where I live. Too b...
-
NoMad11"Do you easily get offended?" "We have a bro culture here. [Some other stuff and examples] So do you think yo...
"What are all the meanings of static in C and C++?"
Not a particularly good question but I think I'll always remember it because of how weirdly specific it is.
(And, anyone who wants to have a go at this WITHOUT Googling, feel free to.)
rant
wk256