CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy C · 7 min · free

Scope, Lifetime and Storage Classes

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Scope is where a variable can be used, lifetime is how long it lives, and storage classes auto, register, static, extern set both.

On screen in this lesson

Three new words

Scope: where in the code a variable can be used
Lifetime: how long the variable stays in memory
Storage class: decides scope, lifetime and default value

Rules of scope

Local: declared inside a block, used only in that block
Global: declared outside all functions, used by all below it
A block is any code inside curly braces { }
Same name inside? The local one hides the global one

Lifetime of variables

VariableCreatedDestroyed
Local (auto)block startsblock ends
Static localprogram startsprogram ends
Globalprogram startsprogram ends

The four storage classes

ClassStored inDefault value
automemory (stack)garbage
registerCPU registergarbage
staticmemory0
externmemory0

Scope and lifetime by class

ClassScopeLifetime
autolocal blocktill block ends
registerlocal blocktill block ends
staticblock or filewhole program
externglobalwhole program

extern in big programs

extern only declares; it does not create memory
The definition is written once, often in another file
Other files write extern to share that variable
Two definitions of the same global give a linker error

Quick answers

How can a function count its calls?

Use a static local variable, which keeps its value between calls.

What is the default value of an auto variable?

Garbage, an unknown leftover value.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. A local variable forgets its value every time a function ends. So how can a function count how many times it was called? Today we learn scope, lifetime and the four storage classes, auto, register, static and extern. By the end, you will know which one to choose.

Let us start with three new words. Scope means the part of the program where a variable's name can be used. Lifetime means how long the variable stays alive in memory while the program runs. A storage class is a keyword that decides a variable's scope, its lifetime and its starting value.

Here total is declared outside all functions, so it is a global variable. Both add and main can use it. The variable bonus is declared inside main, so it is a local variable of main. The function add cannot see bonus at all. Add increases total by fifty, so the output is one hundred fifty and five.

Here are the rules of scope. A local variable is declared inside a block, and it can be used only inside that block. A global variable is declared outside every function, and every function written below it can use it. A block simply means code inside a pair of curly braces. If a local variable has the same name as a global one, the local one hides the global one inside that block.

Pause and predict the output of this program. There are three variables, all named x. Inside the inner curly braces, the nearest x is three, so it prints three. After that block ends, the nearest x is the local x of main, which is two. The global x, which is one, is hidden the whole time. So the output is three, then two.

Now lifetime, which is about time, not place. A normal local variable is created when its block starts, and destroyed when the block ends. A static local variable is created once, and stays alive until the program ends. A global variable also lives for the whole run of the program.

C has four storage classes, and this table shows where each is stored and its default value. Auto variables live in main memory, in an area called the stack, and start with garbage, meaning an unknown leftover value. Register asks the compiler to keep the variable in a CPU register, a tiny, very fast memory in the processor. It also starts with garbage. Static variables are kept in memory for the whole program and start at zero. Extern variables are globals defined somewhere else, and they also start at zero.

Here is the second table exams ask for. Auto has local scope, and lives only until its block ends. Register is the same, local scope, and lives until its block ends. Static can be local to a block or to one file, but it lives for the whole program. Extern has global scope and also lives for the whole program.

Every local variable is auto by default, so writing the word auto is optional. Here sum is auto, and the loop counter i is register. Register is only a request, and modern compilers usually decide on their own. You cannot take the address of a register variable with the ampersand sign. The loop adds one, two, three and four, so it prints ten.

Here is the answer to our opening question. Think of a website visitor counter. Inside visit, count is a static local variable. It is set to zero only once, not on every call. Each call adds one, and the value is remembered for the next call. So three calls print one, two, three. Without the word static, count would restart at zero each time, and it would print one, one, one.

The word extern declares a global variable that is defined somewhere else. Here, the line extern int gst tells the compiler that gst exists. The real definition, int gst equals eighteen, is at the bottom of the file. So main can use gst before it is defined. Two percent signs print one percent sign, so the output is GST eighteen percent.

In real projects, extern is used to share a global variable between files. An extern line only declares the variable, so it does not create new memory. The actual definition is written exactly once, often in another file. Every other file that needs it writes an extern declaration. If two files both define the same global, the linker gives a multiple definition error.

So how do you choose? For normal temporary work inside a function, use auto, which you get by default. To remember a value between calls, use a static local variable. To keep a global variable private to one file, write static before it. To share one global variable across files, define it once and use extern elsewhere. For a counter used very often, you may request register, though compilers usually optimise this already.

Finally, four good habits. Keep variables local, with the smallest scope possible. Use few global variables, because any function can change them, and bugs become hard to find. Always give auto variables a starting value, because they begin with garbage. And use static only when a value truly must be remembered.

Let us revise. Scope is where a variable can be used, and lifetime is how long it lives. A local variable hides a global variable with the same name. Auto and register variables are local and die when their block ends. A static variable keeps its value for the whole program. And extern declares a global variable that is defined elsewhere. Try the counter program with and without static, and predict the output first.

Courses that teach this

CourseUnit
Programming All levels CFunctions and Storage Classes

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →