In programming, a scope is an area of the program where a declared variable can exist but cannot be accessible beyond that variable. In the C programming language, variables can be defined in three places:
Local variables are variables that exist within a function or a block.
Global variables are variables that exist outside of all functions.
Formal parameters are used in the definition of function parameters.
Let's define local and global variables, as well as formal parameters.
Local variables are defined within a function or block, and they can only be utilized by statements that are included within that function or code block. Local variables aren't known to have any effect outside of their scope. The following example demonstrates the usage of local variables. The variables a, b, and c are all local to the function main().
Global variables are declared outside of a function, generally at the beginning of a program. Global variables maintain their values throughout the life of your program and may be accessed from any of the program's functions.
Any function has access to a global variable. Once a global variable is declared, it is available for usage throughout the whole program. The following program demonstrates the use of global variables in a program.
Local and global variables in a program can have the same name, but the value of a local variable inside a function takes precedence. Here's an illustration: