c program to show external variable scope

External variable scope in c language:

Before writing a c program to show external variable scope lets try to understand what is an external variable.

There are four types of storage classes or variable scopes which are Automatic variable scope, register variable scope, Static variable scope and external variable scope.
An external variable is a variable defined outside any function block

An alternative to automatic variable, we can define a variable that is external to all functions. we can access this variable by its name from any function. so, external variable is accessed globally. An external variable can be used instead of argument lists to communicate data between functions. It  remains in existence permanently, rather than appearing and disappearing as functions are called and exited. It retain it’s values even after the functions that set them have returned.

When we define a variable outside all functions by default it is external variable. However we can force scope a variable as external by keyword ‘extern’.

c program to show external variable scope
c program to show external variable scope

write a c program to show external variable scope

write a c program to show external variable scope

#include<stdio.h>
int i;
// The variable ‘i’ acts as global variable can be accessed outside program
void next( void );
int main()
{
i = 3;
printf( “TO SHOW EXTERNAL VARIABLE SCOPE \n” );
i++;
printf( “%d\n”, i );
next();
}

void next( void )
{
i++;
printf( “%d\n”, i );
}

OUTPUT:
TO SHOW EXTERNAL VARIABLE SCOPE
3
4

Related Programs

static variable scope
register variable scope 
 automatic variable scope

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
convert decimal number to octal
decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

 

 

External Links:
Microsoft C Language Reference: The extern Storage-Class Specifier

Leave a Reply

Your email address will not be published. Required fields are marked *