C FunctionsLesson 26Free preview
C Scope
This lesson is part of C · All levels
Summary
Scope determines where a variable can be accessed in a program. Local variables are declared within a function or block {} and only live until the block is complete, whereas global variables are declared outside all functions so that they can be used anywhere in the file. Although it looks global, overuse makes code difficult to track and vulnerable to change unintentionally. The best practice is to use local variables as much as necessary and limit global as necessary.
Key points
- The local variable is only visible within the function or block where it is declared
- The global variable is declared off-function and valid throughout the file
- Local names can override global names, with local values prioritized within the block.
- Limiting scope reduces side effects and facilitates debugging
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.