Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A What does the static keyword do in C?

static is an unfortunately heavily overloaded keyword. At "file scope" Symbols are said to be at "file scope" if their definition is not inside a function. When the static keyword appears at fil...

posted 7mo ago by aghast‭

Answer
#1: Initial revision by user avatar aghast‭ · 2024-05-25T01:57:23Z (7 months ago)
`static` is an unfortunately heavily overloaded keyword.

## At "file scope"

Symbols are said to be at "file scope" if their definition is not inside a function. 

When the `static` keyword appears at file scope, it specifies the **linkage** of the symbol to which it is applied. "Linkage" in this case means "visibility outside the current translation unit." So a variable name or a function name that are *not* declared `static` will default to being visible to outside translation units that may want to link to them ("reference" them, in linker terms). But a symbol marked `static` is *not* visible outside the current translation unit. So at file scope, `static` means `private`.

## At "function scope"

When the `static` keyword appears at function scope -- that is, when it applies to a symbol defined inside a function -- it specifies the **storage duration** of the symbol to which it is applied. You will know that with the exception of GCC, which provides an extension for the purpose, functions cannot be defined inside other functions. So `static` at function scope will be applied only to variables. 

By default, variables at function scope have *automatic* storage duration. This means they are created on the stack, and cease to exist when control leaves the scope of the function. (They are said to be "on the stack" because most CPUs support quickly allocating storage on the stack for this purpose.) 

But variables inside a function that are declared `static` become persistent. Their storage duration is "static," meaning that the value is not stored on the stack. It is stored in the global data area, defaults to 0, is initialized before `main` is called, etc. All the things that are true of global (file scope) variables are also true of `static` variables at function scope. Most importantly, `static` variables in a function hold their value from one call to the next.

## At "parameter scope"

The use of the `static` keyword for parameters is just "we need a keyword, and don't want to introduce a new one." It basically says "the pointer being passed is not null, and points to at least this much storage." It has no relation to the other meanings of `static`.