Thread: In .h files, should function declarations be 'extern' ?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    In .h files, should function declarations be 'extern' ?

    Hi everyone,

    I'm writing a program in C99 that is already more than 30 files after two days of coding. I put all my function definitions in .c files, usually one or a few functions per file. Any functions or variables that are only called from within that file I declare 'static'. Any functions or variables that should be known to other translation units, I copy the function declaration to a .h file, put an include guard on it, and then #include "Blah.h" in whichever other .c file needs to know about the external function/variables.

    Now I think the variable declarations need to be 'extern' or else they'll end up being defined more than once.

    My question is: Should the function declarations be allowed to sit as declarations, or should I 'extern' them ?

    Richard

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    It's entirely up to you whether you put extern in front of function prototypes.

    For me, I prefer the copy/paste approach which minimises the chance of making a mess of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why do I get the impression this is being massively overthought?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

Tags for this Thread