question: typedef's do they belong in header file or in .c files ?

Discussion in 'C Programming' started by jason, Nov 14, 2007.

  1. jason

    jason Guest

    Hello,

    I have a question regarding type definitions. I usually put them in
    header files. But I recently came over several discussions on the www.
    And now I'm not to sure anymore if they go in .c file or in the header
    files ?

    What would be the correct way ?

    Thank you.

    Jas.
     
    jason, Nov 14, 2007
    #1
    1. Advertising

  2. jason

    Dave Hansen Guest

    On Nov 13, 6:59 pm, jason <> wrote:
    > Hello,
    >
    > I have a question regarding type definitions. I usually put them in
    > header files. But I recently came over several discussions on the www.
    > And now I'm not to sure anymore if they go in .c file or in the header
    > files ?
    >
    > What would be the correct way ?


    Header files are for sharing. If you need visibility of the typedef
    across two or more source modules, put it in a header file. If the
    typedef is used in only one source module, put it there.

    Regards,

    -=Dave
     
    Dave Hansen, Nov 14, 2007
    #2
    1. Advertising

  3. "jason" <> wrote in message
    news:473a4882$0$3474$4all.nl...
    >
    > I have a question regarding type definitions. I usually put them in
    > header files. But I recently came over several discussions on the www.
    > And now I'm not to sure anymore if they go in .c file or in the header
    > files ?
    >
    > What would be the correct way ?


    The distinction between .C files and .H files is, from the compiler's point
    of view, arbitrary. You could, if you wanted, put all the code in the .H
    files and the header information in the .C files and most compilers would
    play along with you. From the point of view of being able to compile it, it
    makes no difference. If you just want the code to compile, put typedefs
    anywhere you want.

    However, .H files are used to expose the "interface" of a software module so
    that other software modules may interact with it. The interface may involve
    available functions, global variables, etc. Many times it is necessary to
    have a type defined in order to use the interface.

    Typedefs should go in the .H file if other software modules need to use them
    for necessity or convenience.

    You are definitely doing something wrong if either of the following occur:

    a)You are repeating the same typedef in more than one file.

    b)The compiler doesn't have enough information to check function definitions
    against invocations.

    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Nov 14, 2007
    #3
  4. jason

    jason Guest

    Re: question: typedef's do they belong in header file or in .cfiles ?

    On Tue, 13 Nov 2007 21:13:42 -0500, David T. Ashley wrote:

    > "jason" <> wrote in message
    > news:473a4882$0$3474$4all.nl...
    >>
    >> I have a question regarding type definitions. I usually put them in
    >> header files. But I recently came over several discussions on the www.
    >> And now I'm not to sure anymore if they go in .c file or in the header
    >> files ?
    >>
    >> What would be the correct way ?

    >
    > The distinction between .C files and .H files is, from the compiler's
    > point of view, arbitrary. You could, if you wanted, put all the code in
    > the .H files and the header information in the .C files and most
    > compilers would play along with you. From the point of view of being
    > able to compile it, it makes no difference. If you just want the code
    > to compile, put typedefs anywhere you want.
    >
    > However, .H files are used to expose the "interface" of a software
    > module so that other software modules may interact with it. The
    > interface may involve available functions, global variables, etc. Many
    > times it is necessary to have a type defined in order to use the
    > interface.
    >
    > Typedefs should go in the .H file if other software modules need to use
    > them for necessity or convenience.
    >
    > You are definitely doing something wrong if either of the following
    > occur:
    >
    > a)You are repeating the same typedef in more than one file.
    >
    > b)The compiler doesn't have enough information to check function
    > definitions against invocations.


    Ok that made things clear again; thank you all for the answers and
    explanations.

    Jas.
     
    jason, Nov 14, 2007
    #4
  5. jason

    cr88192 Guest

    "jason" <> wrote in message
    news:473a4882$0$3474$4all.nl...
    > Hello,
    >
    > I have a question regarding type definitions. I usually put them in
    > header files. But I recently came over several discussions on the www.
    > And now I'm not to sure anymore if they go in .c file or in the header
    > files ?
    >
    > What would be the correct way ?
    >
    > Thank you.
    >


    IMO, headerers are better.

    this is also especially true in my case since I tend to use tools to
    autogenerate the headers (function prototypes and so on), and so, not having
    types in headers can make a very big mess with said autogenerated
    prototypes...


    > Jas.
     
    cr88192, Nov 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mylinux
    Replies:
    0
    Views:
    512
    Mylinux
    Sep 17, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    624
    Jack Klein
    Jul 24, 2004
  3. ddog
    Replies:
    3
    Views:
    7,574
  4. Replies:
    4
    Views:
    1,123
    Roedy Green
    May 8, 2008
  5. mlt
    Replies:
    2
    Views:
    881
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page