In programming, you'll often have to deal with the scope of a variable. The scope of a variable determines whether or not you can access and modify it inside a specific block of code. Show
In this tutorial, you'll learn about variable scope in the C programming language. You'll see some code examples to help you understand the differences between local and global variables. What is the Scope of a Variable?Before going ahead to learn about local and global variable scope, let's understand what scope means. In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified. In the next section, you'll learn about local scope of variables Local Scope of Variables in C – Nested BlocksIn this section, you'll learn how local variables work in C. You'll first code a couple of examples, and then you'll generalize the scoping principle. ▶ Here's the first example:
Let's understand what the above program does. In C, you delimit a block of code by
Now, compile and run the above program. Here's the output:
You can see the following:
Local Scope of Variables in C – Nested Blocks Example 2▶ Here's another related example:
If you try compiling the above code, you'll notice that it doesn't compile successfully. And you'll get the following error message:
This is because the variable Based on the above observations, let's write down the following generic principle for local scoping of variables:
Local Scope of Variables in C – Different BlocksIn the previous example, you learned how variables inside the nested inner block cannot be accessed from outside the block. In this section, you'll understand the local scope of variables declared in different blocks.
In the above example,
▶ Now compile and run the above program. You'll get the following error message:
If you notice, on Therefore, the scope of the variable We can represent this notion of local scope generically as follows:
Global Scope of Variables in CSo far, you've learned about local scope of C variables. In this section, you'll learn how you can declare global variables in C. ▶ Let's start with an example.
In the above example,
This program compiles without any error, and the output is shown below:
In this example, there are two functions – the However, the variable This principle of global scope of variables can be summarized as shown below:
Wrapping UpIn this tutorial, you've learned the differences between local and global scope. This is an introductory tutorial on variable scope in C. In C, there are certain access modifiers to control the level of access that the variables have. You can change access by using the corresponding keywords when you declare variables. See you all in the next tutorial. Until then, happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What do you mean by the scope of the variable?In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.
What is the scope of a variable quizlet?The scope of a variable is controlled by the location of the variable declaration, and defines the part of the program where a particular variable is accessible. JavaScript has two scopes - global and local.
What is the term used for the variable that receives the argument being passed into a method?A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.
Which type of variable is visible to every module and the entire program?Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module.
|