12.13 – Need of extern qualifier in C

by subbu on December 26, 2013

Need of extern qualifier:

By default both the data symbols, functions defined in a file have external linkage and can be accessed into any other file but, need to declare with extern storage specifier before we use them. Now we will see what happens if we don’t do so

Accessing external data symbols:

Using Turbo C Compiler tcc:

>edit first.c
int x=45;

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

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

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

In this example ‘x’ is defined as an external variable in the file “first.c” and trying to access from “main.c”. Now we will see what happens when we compile?

>tcc first.c main.c
Turbo C++ Version 3.00 Copyright (c) 1992 Borland International
first.c:
main.c:
Error main.c 4: Undefined symbol 'x' in function main

Yes, external variables belongs to one file can’t be accessed into another file without declaring with the storage specifier extern

Now we will try again by declaring ‘x’ in main.c using extern qualifier

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

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

>tcc main.c first.c
>main
x=45

Now ‘x’ is accessed successfully into the “main.c”

In Linux:

$gedit first.c
int x=45;

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

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

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

In this example ‘x’ is defined as an external variable in the file “first.c” and trying to access from “main.c”. Now we will see what happens when we compile?

$gcc  -o main main.c first.c
main.c: In function 'main' :
main.c:4: error: 'x' undeclared (first use in this function)
main.c:4: error: (Each undeclared identifier is reported only once
                 for each function It appears in.)

Yes, external variables belongs to one file can’t be accessed into another file without declaring with the storage specifier extern

Now we will try again by declaring ‘x’ in main.c

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

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

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

Now ‘x’ is accessed successfully into the main.c

Accessing external functions:

Functions defined in a file are by default external, can be accessed from any C file. Unlike external variables, it is not mandatory to declare the functions using extern storage specifier while accessing from other files. But, it is best practice of declaring external functions using extern specifier.

Now let us see a case where declaration of function with extern specifier is best programming practice

Using turbo C Compiler tcc:

>edit first.c
int sum(int x,int y)
{
 return x+y;
}

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

>edit main.c
#include<stdio.h>
int main()
{
 int c;
 c=sum(10,20,50);
 printf("Sum %d",c);
 return 0;
}

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

>tcc  first.c main.c
>main
Sum 30

Here the main() function is calling the function sum() with 3 arguments that actually takes 2 arguments. It must result an error but compiler could not detect the error.

It can be rectified by declaring the sum() function using the extern storage specifier.

>edit main.c
#include<stdio.h>
int main()
{
extern int sum(int,int);
int c;
c=sum(10,20,30);
printf("Sum %d",c);
return 0;
}

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

>tcc main.c first.c
Error main.c 5: Extra parameter in call to adding in function main

Yes, now we got what we have expected, Now program would be successfully compiled only when 2 arguments are send to sum()

>edit main.c
#include<stdio.h>
int main()
{
 extern int sum(int,int);
 int c;
 c=sum(10,20);
 printf("Sum %d",c);
 return 0;
}

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

>tcc first.c main.c
>main
Sum 30

In Linux:

$gedit first.c
int sum(int x,int y)
{
 return x+y;
}

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

$gedit main.c
#include<stdio.h>
int main()
{
 int c;
 c=sum(10,20,30);
 printf("Sum %d",c);
 return 0;
}

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

$gcc –o main first.c main.c
$./main
Sum 30

Here the main() function is calling the function sum() with 3 arguments that actually takes 2 arguments. It must result an error but compiler could not detect the error.

It can be rectified by declaring the sum() function using the extern storage specifier.

$gedit main.c
#include<stdio.h>
int main()
{
 extern int sum(int,int);
 int c;
 c=sum(10,20,30);
 printf("Sum %d",c);
 return 0;
}

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

$gcc –o main first.c main.c
main.c: In function 'main':
main.c:6: error : too many arguments to function 'adding'
$

Yes, now we got what we have expected, Now program is successfully compiled only when 2 arguments are send to sum()

$gedit main.c
#include<stdio.h>
int main()
{
 extern int sum(int,int);
 int c;
 c=sum(10,20);
 printf("Sum %d",c);
 return 0;
}

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

$gcc –o main first.c main.c
$./main
Sum 30
$

Previous post:

Next post: