Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question: typedef's do they belong in header file or in .c files ?

Reply
Thread Tools

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

 
 
jason
Guest
Posts: n/a
 
      11-14-2007
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.
 
Reply With Quote
 
 
 
 
Dave Hansen
Guest
Posts: n/a
 
      11-14-2007
On Nov 13, 6:59 pm, jason <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
David T. Ashley
Guest
Posts: n/a
 
      11-14-2007
"jason" <(E-Mail Removed)> wrote in message
news:473a4882$0$3474$(E-Mail Removed)4al l.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 ((E-Mail Removed))
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)


 
Reply With Quote
 
jason
Guest
Posts: n/a
 
      11-14-2007
On Tue, 13 Nov 2007 21:13:42 -0500, David T. Ashley wrote:

> "jason" <(E-Mail Removed)> wrote in message
> news:473a4882$0$3474$(E-Mail Removed)4al l.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.
 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-14-2007

"jason" <(E-Mail Removed)> wrote in message
news:473a4882$0$3474$(E-Mail Removed)4al l.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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Loading properties file in .war under JBoss - where do they belong? ddog Java 3 08-04-2007 12:25 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM
Putting your fingers where they don't belong!!! philo Computer Support 6 07-10-2003 02:35 PM



Advertisments