Wednesday, August 27, 2014

Linkage in c

 Translation unit:
  A program in a single file - single translation unit.

sharing functions and variable btw translation units : possible if variables and function have external linkage
by default global variable and non static functions (non static variables also) has external linkage so can be shared

Ex

file1.c                                                file2.c
int a=0;                                              extern int a;   //  refering to file1.c's a
void abc()                                          void ab()
{                                                         {
                                                                  abc();        //
                                                                  notshared();   // error can't access notshared
}                                                         }
static void notshared()                      
{


}

variable and functions which has internal linkage like static functions and variables can't be accessed in other files(Translation units).

No linkage :   variables within function

Linkage :
        1. External
        2. Internal
        3. No linkage

No comments:

Post a Comment