Explain Linkage and Scope of Variables and Functions in C with Examples

«
»
This C Tutorial explains Linkage/Scope of variables and functions in C with examples and defines the type of Linkages.

Linkage of any variable and/or function defines the Area of the Program in which they are accessible. There are three types of Linkages for the variables & functions. These are as follows

1. EXTERNAL
2. INTERNAL
3. NONE

advertisement

External linkage defines that the variable, function can be accessed throughout the program, across the multiple source files if program comprises of several source files with the use of Keyword extern.

#include <stdio.h>
int counter;		    
extern float marks;
 
/*
 * External linkage: counter is an integer accessible across entire program	
 * External linkage: marks which is of float type defined in some other source file & used here
 */
 
void hello(void);
int main()
{
    printf("the marks are %f\n", marks = 55.9);
 
    return 0;
}

Static Keyword changes Linkage from External to Internal meaning that any variable, function declared globally with static keyword can only be accessed within the source file from the point of declaration until the end of the program in which it is declared.

advertisement
#include <stdio.h>
 
/*
 * Internal Linkage: counter can only be accessed within this source file from this point till the end of the file
 * Internal Linkage: function hello can only be accessed within this source file
 */
 
static int counter; 			
static void hello(void); 		 
 
int main(void)
{
    hello();
}

Automatic and register variables neither have external nor internal linkages. They are accessed only within the blocks in which they are declared. For example:

#include <stdio.h>
void hello(void);
int main(void)
{
    hello();
 
    return 0;
}
 
void hello()
{
    int counter = 1;  /* counter is an automatic variable i.e. ONLY accessible within this block! */
 
    printf("the value of counter is %d\n",counter);
}

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn