Thread: Doubt in using either extern or including a header file

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt in using either extern or including a header file

    Hi,

    I have 3 files as below

    a.c:
    Code:
    int x(int a, int b)
    {
       return a + b;
    }
    b.c
    Code:
    extern int x(int,int);
    int main()
    {
         int res;
         res = add(2,3);
         printf("res= %d",res);
         return 0;
    }
    In the file b.c, as function add is defined in a.c, I have used extern declaration for the prototype of the function.

    But instead of this, if I use a header file lets say a.h which contains the prototype of the function x as below

    a.h
    Code:
    int x(int,int);
    then if I include this a.h in b.c, then do I need not place the extern declaration again??

    extern declaration tells the compiler that the function x is defined externally in another file.

    But if I include the a.h simply in b.c, as it copies the contents of the a.h in b.c, then there will be no extern. So how the compiler knows that function x is defined externally somewhere??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    then if I include this a.h in b.c, then do I need not place the extern declaration again??
    Yes.

    Quote Originally Posted by karthik537
    But if I include the a.h simply in b.c, as it copies the contents of the a.h in b.c, then there will be no extern. So how the compiler knows that function x is defined externally somewhere?
    The extern is the default, i.e., your function prototype without the extern specified is equivalent to one with the extern specified.

    By the way, I note that you named your function x but called it as add. I presume this is just a typo error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Thanks laserlight!!.... So without the header file, if I put the forward declaration of function add in b.c file as

    Code:
     int add(int,int);
    without extern, will it work??

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You might like to read section 6.2.2 of the C11 Standard ( http://www.open-std.org/JTC1/sc22/wg...docs/n1570.pdf ), particularly paragraph 4

    For an identifier declared with the storage-class specifier extern in a scope in which a
    prior declaration of that identifier is visible if the prior declaration specifies internal or
    external linkage, the linkage of the identifier at the later declaration is the same as the
    linkage specified at the prior declaration. If no prior declaration is visible, or if the prior
    declaration specifies no linkage, then the identifier has external linkage.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    So without the header file, if I put the forward declaration of function add in b.c file as (...) without extern, will it work??
    Quote Originally Posted by qny
    You might like to read section 6.2.2 of the C11 Standard (...) particularly paragraph 4
    Paragraph 5 is more relevant here.

    Anyway, here's the thing: a declaration in a header file that is identical to a declaration directly in a source file is identical. The header inclusion is pretty much an almost dumb text replacement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. header file including
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 01-16-2011, 08:31 AM
  3. including header file
    By Delia in forum C Programming
    Replies: 1
    Last Post: 03-20-2010, 04:52 PM
  4. C - including the <windows.h> header file
    By Jolu42 in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 01:05 PM
  5. Replies: 4
    Last Post: 12-14-2005, 02:21 PM