Confused by extern decalration/definition in header files.

This is a discussion on Confused by extern decalration/definition in header files. within the C Programming forums, part of the General Programming Boards category; Hi All, In many open source project, the header file contains lines such as: extern int x; extern char y; ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Post Confused by extern decalration/definition in header files.

    Hi All,
    In many open source project, the header file contains lines such as:
    extern int x;
    extern char y;
    ...

    And this .h file is included by another foo.c file, so x and y may be accessed in foo.c. These lines in .h, look like variable declarations, because extern variable definition conforms the forms:
    extern int x=10;
    or
    int x;
    or
    int x = 10;

    If 'extern int x' is declaration, then perhaps the definition should be somewhere else(in another file)? or 'extern int x;' is understood as an external definition?

    thanks..

    tony

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    The actual definition (without the "extern" keyword) must exist in one, and only one, .c file.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 02:42 AM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. need help with menu program and header files
    By DFC in forum C++ Programming
    Replies: 12
    Last Post: 12-07-2005, 11:09 AM
  4. What /what not to put in header files
    By Just in forum C Programming
    Replies: 1
    Last Post: 12-14-2002, 09:45 AM
  5. Header files (Which, when, how,...)
    By zigona in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2001, 10:12 AM

Tags for this Thread


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