why is h file needed?

  • Thread starter Montezuma's Daughter
  • Start date
M

Montezuma's Daughter

Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks
 
V

Victor Bazarov

Montezuma's Daughter said:
I was wondering why h file ia needed?
why can't everything be written in C file?

It can, and it does, in a way. In most cases the existence
of headers (the 'h' stands for 'header') is just to create
convenience. For example, if you want to use your class in
more than one .C file (.C stands for the Unix file extension
commonly used for C++ source file, not C language), you need
the definition of that class to be visible to the compiler
when it compiles a.C and b.C (and all other files that are
compiled separately in your project). You can duplicate the
code in each file, or you can simply place the code in some
other file, call it 'myclass.h' and perform source code
inclusion using the preprocessor directive #include. In
that case, if you need to edit your class, you do it in the
header, one place, instead of going through all definitions
of the same class in all .C files.

Or did I misinterpret your confusion?

V
 
S

Salt_Peter

Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks

We don't disscuss C files, source files are typically *.cpp files in C+
+

Its not needed, depending on what you do. Headers are used to keep
seperate declarations isolated. Thats what *.h files do. If you choose
to declare everything in your source files (ie: *.cpp) then don't be
surprised when you realize that you can't find your declarations in a
mess or reuse the same declarations in multiple translation units.

Should i require a given type in 10 different projects and i already
have a custom type that does the job, i'ld be nuts to rewrite the
types, just copy type.h and type.cpp over. #include "type.h" (done)

So its really a question of whether you want to do it the hard way, or
the easy way. I pprefer the latter.
 
P

Pete Becker

they do, i didn't say .C files

I didn't say that you did. I merely hinted that maybe you were focusing
on the wrong aspect of the orginal question.
 
J

Jim Langston

Montezuma's Daughter said:
Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks

..h files are not needed, they are desired. Consider a program that includes
two different .cpp or .c files. This will normally create two object files.
Consider that one of the source files declares some functions.

int foo( int x ) { /*...*/ };
char* bar() { /* ... */ };

and such. Now, without a header file you will need to declare the
prototypes in the other source file.

int foo( int x );
char* bar();

There can be many functions, structures and the like and you would have to
check the source file and copy lines for each one you wanted to call. This
is where a header file comes in. A header file is basically just a list of
prototypes and structures used in some object file (or library) that you can
include in your source file without having to type them in manually each
time.

If you are only using one source file and no others, you could get away with
not including header files for your code, but would probably still need to
include header files for the system files, stdio.h, memory.h and the like.
These are prototypes and such for system/core functions.
 
P

Pascal Bourguignon

Jim Langston said:
.h files are not needed, they are desired. Consider a program that includes
two different .cpp or .c files. This will normally create two object files.
Consider that one of the source files declares some functions.

int foo( int x ) { /*...*/ };
char* bar() { /* ... */ };

and such. Now, without a header file you will need to declare the
prototypes in the other source file.

int foo( int x );
char* bar();

There can be many functions, structures and the like and you would have to
check the source file and copy lines for each one you wanted to call. This
is where a header file comes in. A header file is basically just a list of
prototypes and structures used in some object file (or library) that you can
include in your source file without having to type them in manually each
time.

If you are only using one source file and no others, you could get away with
not including header files for your code, but would probably still need to
include header files for the system files, stdio.h, memory.h and the like.
These are prototypes and such for system/core functions.

Well, with some IDE, you could have your sources in a database, where
no source or header file would be defined really. You could then
just define the compilation units (or let the IDE do it for you), and
the IDE would generate a single source file for each compilation unit,
containing all the declarations and definitions needed.
 
L

Lars Uffmann

Pete said:
I didn't say that you did. I merely hinted that maybe you were focusing
on the wrong aspect of the orginal question.

He got you there, Salt_Peter :)
 
J

James Kanze

On 2008-01-08 14:15:06 -0500, Salt_Peter <[email protected]> said:
Funny thing, many compilers treat .C files as C++.

I can remember when .C was the only extension used for C++. Of
course, as soon as C++ was ported to MS-DOS, that changed:).
The result is that there really isn't a standard---.cc seems to
be the most widespread convention, at least at my customers, but
.cpp is also common, particular in the Windows world (although
I've never seen it on code not ported to Windows).
 
J

James Kanze

Well, with some IDE, you could have your sources in a
database, where no source or header file would be defined
really. You could then just define the compilation units (or
let the IDE do it for you), and the IDE would generate a
single source file for each compilation unit, containing all
the declarations and definitions needed.

You're doubtlessly thinking of Visual Age.

The standard does require "translation units" (although nothing
says that you have to be able to compile them separately), and
it requires that the declaration of anything used be present in
the translation unit---from what I've heard, visual age wasn't
conform in that regard (but there may have been an option to
make it conform).

More to the point, of course, on a large project, you want to
keep your interface specification in a separate file from the
implementation to begin with.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top