 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- 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
Advertisements
 