Question

Here I have two files externdemo1.c and externdemo2.c.In the first file,I have declared and initialized a character array arr at file scope.But I have declared it in the second file externdemo2.c without the extern keyword and made use of it there in the function display(). Here are my confusions arising from it.Please answer these three:

 //File No.1--externdemo1.c

#include<stdio.h>
#include "externdemo2.c"

extern int display();
char arr[3]={'3','4','7'};
//extern char arr[3]={'3','4','7'};

//extern int main()
int main()
{
    printf("%d",display());
}


//File No.2--externdemo2.c

char arr[3];

int display()
{
    return sizeof(arr);
}

1) Why does the program compile fine even though I have declared arr without the extern keyword in externdemo2.c?I had read that the default linkage of functions is external,but I am not sure if that's so even for variables.I only know that global variables have extern storage class.

2) What is the rigorous difference between extern storage class and extern linkage.I badly need a clarification about this.In the first file,where I have defined the array arr,I haven't used the keyword extern, but I know that it has extern storage class by default.But in the second file, isn't there any default extern ,storage class or linkage,about the global variable arr,ie, in externdemo2.c?

3) Check the commented out line in the first file externdemo1.c.Just to test it, I had used the line extern char arr[3]={'3','4','7'};.But it gives the error 'arr' initialized and declared 'extern'.What does this error mean? I have also mentioned a commented line extern int main(),but it works fine without error or warning.So why can we use extern for a function even though a function is extern by default,but not for a variable,like arr here?

Please take some time to bail me out over this.It will clear most of my lingering doubts about the whole extern thing.It will be immense help if you can answer all 3 bits 1),2) and 3). Especially 3) is eating my brains out

Was it helpful?

Solution

Main questions

  1. Basically, because you've included the source of externdemo2.c in the file externdemo1.c.

  2. This is the big question. Because there is no initializer, the line char arr[3]; in externdemo2.c generates a tentative definition of the array arr. When the actual definition with initialization is encountered, the tentative definition is no longer tentative — but neither is it a duplicate definition.

    Regarding extern storage class vs extern linkage...Linkage refers to whether a symbol can be seen from outside the source file in which it is defined. A symbol with extern linkage can be accessed by name by other source files in which it is appropriately declared. To the extent it is defined, extern storage class means 'stored outside of the scope of a function', so independent of any function. The variable defined with exern storage class might or might not have extern linkage.

    Because it is not defined with the keyword static, the array arr has extern linkage; it is a global variable.

  3. With the commented out line uncommented out, you have two definitions of one array, which is not allowed.

I observe that you must be compiling just externdemo1.c to create a program — the compiler is including the code from externdemo2.c because it is directly included. You can create an object file from externdemo2.c. However, you cannot create a program by linking the object files from both externdemo1.c and externdemo2.c because that would lead to multiple definitions of the function display().

Auxilliary questions

I have placed both files in the [same directory]. If I don't include the second file in the first, then when I compile the first file it gives the error undefined reference to display. Since I have used extern for that function in the first file, isn't the linker supposed to link to it even if I don't include the second file? Or the linker looks for it only in default folders?

There are a couple of confusions here. Let's try dealing with them one at a time.

Linking

The linker (usually launched by the compiler) will link the object files and libraries that are specified on its command line. If you want two object files, call them externdemo1.obj and externdemo2.obj, linked together, you must tell the linker (via the build system in the IDE) that it needs to process both object files — as well as any libraries that it doesn't pick up by default. (The Standard C library, plus the platform-specific extensions, are normally picked up automatically, unless you go out of your way to stop that happening.)

The linker is not obliged to spend any time looking for stray object files that might satisfy references; indeed, it is expected to link only those object files and libraries that it is told to link and not add others at its whim. There are some caveats about libraries (the linker might add some libraries not mentioned on the command line if one of the libraries it is told to link with has references built into it to other libraries), but the linker doesn't add extra object files to the mix.

C++ with template instantiation might be argued to be a bit different, but it is actually following much the same rules.

Source code

You should have a header, externdemo.h, that contains:

#ifndef EXTERNDEMO_H_INCLUDED
#define EXTERNDEMO_H_INCLUDED

extern int display(void);

extern char arr[3];  // Or extern char arr[]; -- but NOT extern char *arr;

#endif /* EXTERNDEMO_H_INCLUDED */

You should then modify the source files to include the header:

//File No.1--externdemo1.c

#include <stdio.h>
#include "externdemo.h"

char arr[3] = { '3', '4', '7' };

int main(void)
{
    printf("%d\n", display());
    return 0;
}

and:

//File No.2--externdemo2.c

#include "externdemo.h"

int display(void)
{
    return sizeof(arr);
}

The only tricky issue here is 'does externdemo2.c really know the size of arr?' The answer is 'Yes' (at least using GCC 4.7.1 on Mac OS X 10.8.3). However, if the extern declaration in the header did not include the size (extern char arr[];), you would get compilation errors such as:

externdemo2.c: In function ‘display’:
externdemo2.c:7:18: error: invalid application of ‘sizeof’ to incomplete type ‘char[]’
externdemo2.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]

OTHER TIPS

Your program looks a bit err. To me the #include "externdemo2.c" line appears invalid.

Following is the correction I have made and it works.

    //File No.1--externdemo1.c

    #include <stdio.h>

    extern char arr[3];

    extern int display();

    int main()
    {
        printf("%d", arr[0]);
        printf("%d",display());
    }

    //File No.2--externdemo2.c


    char arr[3]={'3','4','7'};

    int display()
    {
        return sizeof(arr);
    }

Please follow the below links for better understanding:

Using #include as shown will make both as one file only. You can check the intermediate file with flag -E, as in:

gcc -E externdemo1.c
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top