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.

Comments on What does the static keyword do in C?

Parent

What does the static keyword do in C?

+15
−0

What exactly does the static keyword do in C? I have seen it used in several diverse contexts:

1) As a variable outside a function:

#include <stdio.h>

static int x = 5;
int main (void)
{
  printf("%d\n", x);
}

2) As a variable inside a function:

void func (void)
{
  static int count=0;
  printf("%d\n", count++);
}

3) As part of a function declaration/definition:

static void func (void);

4) As part of a function parameter list containing an array:

void func (int arr[static 5]);

All of these uses seem quite unrelated. What exactly is the purpose of static in all the above examples?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Keyword overloading confusion (1 comment)
Post
+1
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Wrong terminology (2 comments)
Wrong terminology
Lundin‭ wrote 5 months ago

Function scope is a formal term in C and it explicitly refers to labels, for the purpose of goto. The correct term for local variables declared inside a function is block scope. And what you call "parameter scope" is formally called function prototype scope and only applies to (prototype format) function declarations, not function definitions.

Lundin‭ wrote 5 months ago

Also, variables declared static at file scope have static storage duration too, not just those at block scope. So static always means static storage duration no matter the scope.