Thursday, 6 May 2021

Scope of variables in C

In C all constants and variables have defined scope. By scope mean that the accessibility and visibility at different points in the program. A variable or constant in C has four types of scopes.

  1. Local scope / block scope
  2. Global scope / Program scope
  3. Function Scope
  4. File scope

 


 

1.Local scope / block scope

Local variables are visible within a block or function where they are declared. They are not accessible to outside of the block. If a variable is declared within a block then as soon as the control exit that block the variable will erased. Such variables are known as local variables and it's have local scope.

2. Global scope / Program scope

C allows us to declare a variable at global deceleration section and use any where throughout the program. Such variables are known as global variables and they have program scope or global scope.

3. Function Scope

Function scope indicates that a variable is active and visible from the beginning to the end of the function. In C only the goto label have function scope. This means that the programmer can't have the same label name inside a function.

4. File scope

When a global variable is accessible until the end of the file, the variable is said ti be have file scope. To allow a variable have file scope, declare that variable with the "static" keyword before specifying  it's data type.

Example: static int a=10;

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top