Doubt in using either extern or including a header file

This is a discussion on Doubt in using either extern or including a header file within the C Programming forums, part of the General Programming Boards category; Hi, I have 3 files as below a.c: Code: int x(int a, int b) { return a + b; } ...

  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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21