Beginner · Functions and arrays · topic 12 of 60 · 25 min
Scope and storage classes
Explanation
Local variables
A variable declared inside a function or block is local: it can be used only inside that block (its scope), and it exists only while the block runs (automatic lifetime). Each call gets fresh local variables. Locals are not initialised automatically, so always give them a value.
void count_calls(void) {
int calls = 0; /* a new variable every call */
calls++;
printf("%d\n", calls); /* always prints 1 */
}static local variables
void count_calls(void) {
static int calls = 0; /* created once, keeps its value between calls */
calls++;
printf("%d\n", calls); /* 1, then 2, then 3 … */
}Global variables
int total_orders = 0; /* global: visible from here to the end of the file, lives forever */
void place_order(void) { total_orders++; }Globals are automatically initialised to 0. They are easy to use but hard to reason about: any function can change them, which makes bugs and testing harder, and they are unsafe with threads. Prefer passing values as parameters and returning results.
Shadowing
int rate = 18; /* global */
void show(void) {
int rate = 5; /* local shadows the global inside this function */
printf("%d\n", rate); /* 5 */
}static and extern for files
Big programs are split into several .c files (module 8). static before a global variable or a function makes it private to its file (internal linkage), so other files cannot see or clash with it. extern declares a global that is defined in another file, so this file can use it.
/* tax.c */
int gst_rate = 18; /* defined here, visible to other files */
static double round2(double x) { … } /* private helper, only tax.c can call it */
/* main.c */
extern int gst_rate; /* declared: "it exists in another file" */| Declaration | Scope (where the name works) | Lifetime |
|---|---|---|
| int x; inside a function | That block | One call (automatic) |
| static int x; inside a function | That block | Whole program |
| int x; outside functions | The file (and other files via extern) | Whole program |
| static int x; outside functions | This file only | Whole program |
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Scope and storage classes · 1/6
In this lesson you will learn where variables can be used and how long they live.
Prefer a video by a teacher? Find free videos on “c storage classes static extern auto scope lifetime” on YouTube ↗
In real life: Generating receipt numbers
A shop's billing module numbers receipts with a static counter inside next_receipt_no(). No other part of the program can change the counter by mistake, and it keeps counting across calls.
int next_receipt_no(void) {
static int last = 1000;
return ++last; /* 1001, 1002, 1003 … */
}Practice (0/3 done)
What does this print? void f(void) { static int n = 0; int m = 0; n++; m++; printf("%d%d ", n, m); } … f(); f(); f();
What does static do on a function defined at file level?
Write a function int next_token(void) that returns 1, 2, 3, … on successive calls using a static local, and a function void reset_hint(void) explaining in a comment why it cannot reset that counter. Then rewrite it with a file-level static variable and a reset function, and say which design you prefer and why.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- RecursionFunctions and arrays · same module
- ArraysFunctions and arrays · same module
- Stack and heapDynamic memory
- Splitting a program into filesThe preprocessor, multi-file programs and make
- Scope: where variables livePython Programming · Functions
- LifetimesRust Programming · Borrowing, slices and lifetimes
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the initial value of a global int with no initialiser?
Answer: 0
Objects with static storage duration are zero-initialised.
What does extern int count; mean?
Answer: count is defined somewhere else (another file)
A declaration, not a definition.
A local variable with the same name as a global…
Answer: Shadows the global inside its block
Inside the block, the local wins.
How long does a static local variable live?
Answer: The whole program run
Static storage duration.
Where do uninitialised global variables live?Related
What does extern int count; in a header mean?Related
Answer: count is defined in some .c file; this just declares it
A declaration without storage.
A variable is created inside a function. Where can you use it?Related
Answer: Only inside that function
Variables made inside a function are local to it and disappear when the function ends.
The compiler suggests adding 'static to a returned reference to a local String. What is usually the right fix?Related
Answer: Return the owned String instead
A local String is dropped at the end of the function; returning ownership is the clean fix.
From Lifetimes
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Functions and arrays” before its exam: Recursion, Arrays, 2D arrays and classic array algorithms.
- Module 3 exam: Functions and arrays · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Strings in C. 60 topics left in the course before the final exam.