What variable scope means in C, and the difference between local variables, which only the function that creates them can use, and global variables, which every function can reach. From the lesson "Variables and scope" in C programming basics (CS50 Shorts). Source: CS50 Shorts, "Variables and Scope" by CS50, Harvard University Original: https://cs50.harvard.edu/x/2024/shorts/variables_and_scope/ Licence: CC BY-NC-SA 4.0 (https://creativecommons.org/licenses/by-nc-sa/4.0/) Changes: Cut from 0:05 to 0:47, reformatted to vertical with a blurred background and re-encoded; captions, summary and thumbnail by Upgram. Not endorsed by CS50 or Harvard University. Shared under the same licence.
You’ll learn how variable scope works in C and how local variables differ from global variables. This is useful when writing functions, because scope determines which parts of a program can access a variable. Local variables belong to the function where they’re created, while global variables are declared outside functions and can be accessed throughout the program.
As you start working with functions, another thing is going to become really important to understand, which is the concept of variable scope. So scope is a characteristic of a variable that defines from which functions that variable can be accessed. There are two primary scopes in C, local variables and global variables. Now, local variables can only be accessed within the functions in which they're created. They can't be accessed by every other function that exists in your program, only the function in which it was created. Global variables, on the other hand, can be accessed by any function in the program. And the reason for that is because they're not created inside of any particular function. We declare them outside of all of the functions, which means that every function knows where it is and can access.
Շարունակելու համար ներբեռնեք հավելվածը
Հավանեք, մեկնաբանեք, հետևեք հեղինակներին և վերահսկեք Ձեր առաջընթացը Upgram հավելվածում։
Ներբեռնել Upgram-ը
Մեկնաբանություններ