C

1. Local Variables

These are declared inside a function or a block (between { }).

  • Scope: Only accessible within the function where they are defined.

  • Lifetime: They are created when the function starts and destroyed when it finishes.

  • Default Value: Contains "garbage" (random data) unless initialized.

2. Global Variables

These are declared outside of all functions, usually at the top of the program.

  • Scope: Accessible by any function throughout the entire program.

  • Lifetime: They exist for the entire duration of the program.

  • Default Value: Automatically initialized to 0.

3. Static Variables

Static variables are declared using the static keyword. They have a unique behavior:

  • Scope: If local, they stay within the function; if global, they stay within that specific file.

  • Lifetime: They retain their value between function calls. They are initialized only once.

4. External (extern) Variables

Used when you want to share a variable across multiple files. The extern keyword tells the compiler that the variable is defined in another file.

Summary Comparison Table

Variable TypeKeywordDeclaration LocationScopeLifetime
LocalNone (default)Inside a functionFunction-levelUntil function ends
GlobalNone (default)Outside all functionsProgram-wideUntil program ends
StaticstaticInside or OutsideLocal/GlobalUntil program ends
Externalextern
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now