UPI Study › Courses › Programming in C › Free lessons › Lesson 6.4 Free with a free account
Variable Storage Classes in C
Programming in C · Chapter 6: Functions, Scope, Storage Classes, and RecursionSection 6.4 · lesson 36 of 786 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Pass by Value vs. Pass by Reference in C →← Variable Scope in CNext lesson: Pass by Value vs. Pass by Reference in C →
In this lesson
How long C variables live with auto, static, register and extern.
- Compare the lifetime of auto locals and static locals
- Explain the default starting value of static and global variables
- Describe the register hint and its address restriction
- Use extern to share a global and static to hide one in its file
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (789 words). Read along, or search it.
Opening Analogy
Credits Reset, High Scores Remain
An arcade machine shows credits, zero, every time you walk up to it. But the high-score board behind the counter remembers the best score from yesterday, last week, even last year. Both are numbers in memory. The difference is how long each one is kept. In C, you choose that with a storage class. Scope, from lesson 6.3, was where a name works. This lesson is about how long the box lives.
auto Versus static
Fresh Boxes Versus Kept Values
Every local you've written so far is an auto variable, short for automatic. C creates its box when the function starts, and discards it at the end. That's why the call stack grows and shrinks. The keyword auto is allowed, but nobody writes it, because it's already the default for locals. Disposable boxes have a catch: each call gets a fresh box, so nothing is remembered from last time. Now put static in front of a local. Its box moves to static storage: set up once before main starts, and kept until the program ends. It's still private to its function, so its scope doesn't change. But it keeps its value between calls. Both functions start count at 0 and add one, and main calls each of them three times. Run it. The auto counter prints 1 every time. The static counter prints 1, 2, 3. The line static int count equals 0 doesn't run on every call. The 0 is placed once, before the program starts. After that, only count plus plus changes it.
Default Values
Static Locals and Globals Start at Zero
A static local with no starting value at all begins at 0. So does a global. That's unlike an ordinary local, whose starting value is leftover garbage, as you saw in lesson 3.2. Run it. total_wins starts at 0, and calls counts 1, then 2.
register
A Speed Hint With No Address
The keyword register asks the compiler to keep a variable in the processor's fastest spot, like a loop counter. It's only a hint, and modern compilers decide this on their own. Used politely, it simply works: the sum of 1 to 100 is 5050. But register brings one hard rule. You can't ask for its address, so the ampersand operator isn't allowed on it. Try it, and clang refuses: address of register variable requested. You'll use ampersand on ordinary variables in lesson 6.5.
Multiple Files
Extern Shares, Static Hides
Big programs live in several .c files. Two keywords control how those files see each other's globals. On the right, tokens.c defines arcade_tokens: the one real box. On the left, main.c says extern int arcade_tokens. Extern means: this variable already exists, and its real box is defined somewhere else. It makes no new box, so both files share one number. Build both files into one program, and run. 500 tokens, and the code check says 1, for yes. Static on a global means the opposite: keep this private to this file. secret_code is static, so only tokens.c can see it. Main reaches for it with extern anyway. Build again, and the linker refuses: undefined symbols, secret_code. The compiler accepted main.c on trust. But the linker, which joins the files, found no visible secret_code to connect it to. A static function is private to its file too. And if two files both define one global, the linker refuses that as well: duplicate symbol.
Summary Table
Four Kinds of Storage Compared
Here are the four kinds together. An auto local lives on the stack for one call, and only its block can use it. A static local lasts the whole run, but only its own function can use it. A global lasts the whole run, and other files can reach it with extern. And a static global lasts the whole run, but stays inside its own file. Register only adds a hint to any of this.
Quick Check
A Static Ticket Counter
Quick check. next_ticket hands out ticket numbers. Its number starts at 100, and each call adds 5. Main calls it three times. What does it print? Pause and predict. Build and run: 105, 110, 115. The static number is set to 100 once, before the program starts. The first call adds 5 and returns 105. The second call finds 105 still there, and adds 5 again. Without static, every call would make a fresh 100, and print 105 three times.
Wrap-Up
Counting Visitors With a Static Local
Back at the arcade, Priya wanted a visitors today counter. As a local, it always showed 1. As a global, any function could reset it. Sam suggested a static local inside the welcome function. It counted correctly, and nothing else could touch it.
Key Takeaways
Storage Classes in Review
To sum up. An auto local gets a fresh box at every call. A static local is set up once, and keeps its value between calls. Register is only a hint, and its address can't be taken. And extern shares one global between files, while static on a global hides it in its file.
About Programming in C at UPI Study
Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
Where the credit goes
Universities that work with NCCRS
Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Variable Storage Classes in C
What is the difference between an auto and a static local variable in C?
What is the initial value of a static variable in C?
What does the extern keyword do in C?
Can you take the address of a register variable in C?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Programming in C.
78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →