auto and Local Scope
Block-level variables.
auto and Local Scope is a free C Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
What Is Scope
Scope is the region of code where a name is visible. In C, variables declared inside a block are visible only within that block.
#include <stdio.h>
int main(void) {
int x = 5;
printf("x = %d\n", x);
return 0;
}The auto Storage Class
Local variables are auto by default. The keyword is rarely written because it is implied for every block-scope variable.
#include <stdio.h>
int main(void) {
auto int n = 42;
printf("n = %d\n", n);
return 0;
}Block Scope
A pair of braces creates a new block. Variables inside it disappear when the block ends.
#include <stdio.h>
int main(void) {
{
int inner = 10;
printf("inner = %d\n", inner);
}
return 0;
}Automatic Lifetime
An auto variable is created when its block is entered and destroyed when the block is exited. This is its lifetime.
#include <stdio.h>
int main(void) {
for (int i = 0; i < 2; i++) {
int fresh = i * 10;
printf("fresh = %d\n", fresh);
}
return 0;
}Shadowing
An inner variable can shadow an outer one with the same name. The inner name wins inside its block.
#include <stdio.h>
int main(void) {
int x = 1;
{
int x = 99;
printf("inner x = %d\n", x);
}
printf("outer x = %d\n", x);
return 0;
}Function Parameters Are Local
Parameters are local variables too. They live only while the function runs.
#include <stdio.h>
int square(int n) {
return n * n;
}
int main(void) {
printf("%d\n", square(6));
return 0;
}Uninitialized Locals
An auto variable is not zeroed automatically. Reading it before assignment gives garbage. Always initialize.
#include <stdio.h>
int main(void) {
int x = 0;
x = x + 5;
printf("x = %d\n", x);
return 0;
}Locals Per Call
Each call to a function gets its own fresh copy of its local variables.
#include <stdio.h>
void greet(void) {
int count = 1;
printf("count = %d\n", count);
}
int main(void) {
greet();
greet();
return 0;
}Why Locals Are Good
Local scope keeps variables contained, avoiding accidental interference between parts of a program.
#include <stdio.h>
int main(void) {
int a = 3;
{
int b = 4;
printf("a + b = %d\n", a + b);
}
return 0;
}Nested Blocks
Blocks can nest. Inner blocks can read outer variables but outer blocks cannot read inner ones.
#include <stdio.h>
int main(void) {
int outer = 1;
{
int middle = 2;
{
int inner = 3;
printf("%d\n", outer + middle + inner);
}
}
return 0;
}Scope and Loops
A variable declared in a for header is scoped to the loop only.
#include <stdio.h>
int main(void) {
int sum = 0;
for (int i = 1; i <= 4; i++)
sum += i;
printf("sum = %d\n", sum);
return 0;
}Quick Check
Test your understanding of auto and local scope.
Recap: auto and Local Scope
You learned how local variables behave.
- Block-scope variables are
autoby default - Their lifetime is tied to the block
- Inner names can shadow outer ones
- Locals are not auto-initialized
- Each function call gets fresh locals
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “auto and Local Scope” lesson free?
Yes — the full text of “auto and Local Scope” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “auto and Local Scope”?
Block-level variables. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “auto and Local Scope” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- auto and Local Scope
- static Variables
- extern and Global
- register and const