12.14 – Internal linkage in C language

by subbu on December 27, 2013

Internal linkage in C language using static qualifier:

Global variables and functions are by default external, linked externally into any file. We can restrict these symbols to internal linkage by defining them with static. These symbols can be only used within the file but, can’t be accessed from other files.

Internal linkage in C language

In the above example variable ‘x’ and function display() are static symbols. These may be accessed only within “File2.c” but, can’t be accessed into “File1.c”
Variable ‘y’ and function show() are non-static symbols. These may be accessed within “File2.c” and as well into “File1.c”

Now we will see an example to demonstrate the fact.

Using Turbo C Compiler tcc:

>edit first.c
#include<stdio.h>
static int x=45;
static void display()
{
 printf("\nHello World");
}

Select Save (Alt+S), Exit (X)

>edit main.c
#include<stdio.h>
extern int x;
extern void display();
int main()
{
 printf("\n%d",x);
 display();
 return 0;
}

Select Save (Alt+S), Exit (X)

>tcc first.c main.c
Turbo C++ Version 3.00 Copyright (c) 1992 Borland International
first.c:
main.c:
Turbo Link  Version 5.0 Copyright (c) 1992 Borland International
Error: Undefined symbol _display in module main.c
Error: Undefined symbol _x in module main.c

By this, it is clear that static external variables and functions can’t be accessed into other files.

>edit first.c
#include<stdio.h>
static int x=45;
static void display()
{
 printf("\nHello World");
}
void show()
{
 printf("\n%d",x);
 display("\nHello World");
}

Select Save (Alt+S), Exit (X)

>edit main.c
#include<stdio.h>
external void show();
int main()
{
 show();
 return 0;
}

Select Save (Alt+S), Quit (Q)

>tcc first.c main.c
>main
45
Helllo World

Here, variable x, functions display() are internal linked symbols and the function show() is external linked symbol. show() can be accessed from other files, in other hand internal linked symbols x, display() can be accessed from show()

In Linux:

$gedit first.c
#include<stdio.h>
static int x=45;
static void display()
{
  printf("\nHello World");
}

Select Save (Alt+S), Quit (Q)

$gedit main.c
#include<stdio.h>
extern int x;
extern void display();
int main()
{
 printf("\n%d",x);
 display();
 return 0;
}

Select Save (Alt+S), Quit (Q)

$gcc –o main main.c first.c
/tmp/cco90A7f.o: In function 'main':
main.c: (.text+0xb): undefined reference to 'x'
main.c: (.text+0x21): undefined reference to 'display'
collect2: ld returned 1 exit status
$

By this, it is clear that static external variables and functions can’t be accessed into other files.

$gedit first.c
#include<stdio.h>
static int x=45;
static void display()
{
 printf("\nHello World");
}
void show()
{
 printf("x=%d",x);
 display("\nHello World");
}

Select Save (Alt+S), Quit (Q)

$gedit main.c
#include<stdio.h>
external void show();
int main()
{
 show();
 return 0;
}

Select Save (Alt+S), Quit (Q)

$gcc –o main main.c first.c
$./main
x=45
Helllo World
$

Here, variable x, functions display() are internal linked symbols and the function show() is external linked symbol. show() can be accessed from other files, in other hand internal linked symbols x, display() can be accessed from show()

Previous post:

Next post: