- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain scope of a variable in C language.
Storage classes specify the scope, lifetime and binding of variables.
To fully define a variable, one needs to mention not only its ‘type’ but also its storage class.
A variable name identifies some physical location within computer memory, where a collection of bits are allocated for storing values of variable.
Storage class tells us the following factors −
- Where the variable is stored (in memory or cpu register)?
- What will be the initial value of variable, if nothing is initialized?
- What is the scope of variable (where it can be accessed)?
- What is the life of a variable?
Scope
Scope defines the visibility of an object. It defines where an object can be accessed.
The scope variable is local or global
- The variable defined within the block has local scope. They are visible only to the block in which they are defined.
- The variable defined in global area is visible from their definition until the end of program. It is visible everywhere in program.
Example
Following is the C program for the scope of a variable −
#include<stdio.h> int c= 30; /* global area */ main ( ) { int a = 10; //local scope// printf ("a=%d,c=%d"a,c); fun ( ); } fun ( ){ printf ("c=%d",c); //global variable }
Output
When the above program is executed, it produces the following output −
a =10, c = 30 c = 30
Example
Following is the C program for the local and global variables −
#include<stdio.h> int a,b; a=1,b=2; main() { int c,d; printf("enter c and d values:"); scanf("%d%d",&c,&d); c=c+d; //local variables b=a*b; //global variables printf("c value is:%d
",c); printf("b value is:%d
",b); }
Output
When the above program is executed, it produces the following output −
enter c and d values:4 7 c value is:11 b value is:2
- Related Articles
- Explain Lifetime of a variable in C language.
- Explain Binding of a variable in C language.
- Explain python namespace and scope of a variable.
- Explain scope rules related to the functions in C language
- Explain variable / Array scope in PowerShell function.
- Explain the accessing of structure variable in C language
- Explain variable declaration and rules of variables in C language
- Explain the scope rules related to the statement blocks in C language
- Explain the variable declaration, initialization and assignment in C language
- PHP Variable Scope
- Write a structure in local scope program using C language
- What is a structure at local scope in C language?
- What is the scope of an internal variable of a class in C#?
- What is the scope of a public member variable of a class in C#?
- What is the scope of a private member variable of a class in C#?

Advertisements