← Back to C Course | Chapter 5: Functions | Lesson 6 of 8

C Scope & Lifetime

Scope is where a name can be seen, like a secret you can whisper only in one room. Variables made inside a block vanish when it ends.
Syntax
c
type global_name = value;  // global scope

void function_name() {
  type local_name = value;  // local scope, exists only in this function
}

Local Scope

A variable declared inside a function has local scope, meaning it's only visible to code within that same function -- code in other functions has no way to reference it, even by the same name.

Example: Local Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
	// Declare local and set it to 5
	int local = 5;
	// Print formatted text to the screen
	printf("%d", local);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Global Scope

A variable declared outside of every function has global scope, making it visible to and modifiable by any function in the program -- convenient for shared state, but riskier since any function could change its value unexpectedly.

Example: Global Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare counter and set it to 10
int counter = 10;
void show() {
	// Print formatted text to the screen
	printf("%d", counter);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Block Scope

Variables declared inside an if block or loop have block scope, meaning they only exist and are only accessible within that specific pair of curly braces, disappearing once execution leaves that block.

Example: Block Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Declare x and set it to 5
		int x = 5;
		// Print formatted text to the screen
		printf("%d", x);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Lifetime

A variable's lifetime is how long its memory allocation persists during the program's execution -- a local variable's memory is reclaimed automatically the moment its enclosing function returns, and its old value becomes inaccessible.

Example: Variable Lifetime

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%d", x);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Name Hiding

If a local variable shares a name with a global variable, C lets the local one take precedence inside its own scope, effectively hiding the global's value there -- the global is unaffected and still accessible from other functions.

Example: Variable Name Hiding

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare value and set it to 100
int value = 100;
void show() {
	// Declare value and set it to 5
	int value = 5;
	// Print formatted text to the screen
	printf("%d", value);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a local variable outside the block where it was declared.
  2. Shadowing a global with a local of the same name and changing the wrong one.
  3. Expecting a local variable to keep its value between calls, when it is reset unless declared static.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.