12.10 – External linkage with Turbo C

by subbu on December 23, 2013

External linkage with Turbo C:

If we have a very large program, then it is a better programming practice to divide different sections of the program as different files and linked together. It is generally done in a couple of ways that are,

Defining different sections of program as different header files and including them into the main program as discoursed in the previous sessions. The advantage with this is, header files developed for one application can be reused in another application. The disadvantage with this is, required a bit of planning and time to implement

The second method is writing different sections of program as different C files and linking them directly without using any preprocessor include statements. The disadvantage with this method is lake of reusability. It can be done in a couple of ways that are,

  1. External linkage using tcc (Turbo C Compiler)
  2. Creating a project using Turbo C IDE

External linkage in Turbo C

Here we divide different sections of the program as different C files and then linked to the main program with tcc (Turbo C Compiler)
The output of the tcc would be the executable code of all the collection

Note: Before going further, make sure that the path is set to c:\tc\bin as explained in the previous session 1.2 How to install Turbo C in Windows XP/Vista. So that, the tcc can be accessed from any where.

Setting path to bin

Step 1: Create or change to any working folder (d:\fox)

Navigation:
Go to the DOS shell by selecting start – run – type cmd – open
Change to d:
Create a folder fox using the command md (Making directory)
Change to fox folder using cd (Changing Directory)

c:\...\...>d:
d:\>md fox
d:\>cd fox
d:\fox>

Creating working folder for lib

Step 2: Creating function libraries and saving them with .c extension.
Here file “one.c” has a function called factorial(), which accepts any integer and returns its factorial

d:\fox>edit one.c
/* accepts an integer and returns it's factorial */
long factorial(int n)
{
 long f;
 int i;
 for(f=i=1;i<=n;i++)
   f=f*i;
 return f;
}

Select Save (Alt+S), Exit (X) to come out of the text editor

Here file “two.c” has a function called isStrong(), which accepts an integer and returns 1 (true) if the number is a strong number otherwise returns 0 (false). This function uses function factorial() of file “one.c”

d:\fox>edit two.c
/* Accepts an integer and returns true(1) if the number
is a strong number otherwise returns false(0)        */
int isStrong(int n)
{
 int temp;
 long sum;
 for(temp=n,sum=0;n!=0;n=n/10)
   sum=sum+factorial(n%10);
 return ((sum==temp)?1:0);
}

Select Save (Alt+S), Exit (X) to come out of the text editor

Here file “main.c” uses the function isStrong() of file “two.c” and prints strong numbers from 1 to the given limit

d:\fox>edit main.c
/* program to print armstrong numbers
from 1 to n                       */
#include<stdio.h>
int main()
{
 int i,n;
 printf("Enter the limit:");
 scanf("%d",&n);
 printf("Strong numbers:\n");
 for(i=1;i<=n;i++)
  if(isStrong(i))
    printf("\n%d",i);
 return 0;
}

Select Save (Alt+S), Exit (X) to come out of the text editor

Step 3: Compiling all the files using tcc
tcc first compiles all the .c files and generates equivalent object files as one.obj, two.obj and main.obj
tcc also performs external linkage of all the object files and generates executable file with the file name which has main() function

d:\fox>tcc main.c one.c two.c

External linking in Turbo C

Step 4: Executable file generated by the tcc can be executed either by typing the name of executable file at the command prompt or by double clicking on executable file in windows mode.

d:\fox>main
Enter the limit:5000
Strong numbers:
1
2
145

Executing a C program

Conclusion:
Here three files are compiled, linked and executed at a time without including one file into another. It is automatically done by the tcc called external linkage.

{ 0 comments… add one now }

Previous post:

Next post: