By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

why is h file needed?

P: n/a
Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks
Jan 8 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Montezuma's Daughter wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 8 '08 #2

P: n/a
On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
wrote:
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.
Jan 8 '08 #3

P: n/a
On 2008-01-08 14:15:06 -0500, Salt_Peter <pj*****@yahoo.comsaid:
On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
wrote:
>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+
+
Funny thing, many compilers treat .C files as C++.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 8 '08 #4

P: n/a
On Jan 8, 2:20 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.comsaid:
On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
wrote:
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+
+

Funny thing, many compilers treat .C files as C++.
they do, i didn't say .C files
>
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Jan 8 '08 #5

P: n/a
On 2008-01-08 14:38:00 -0500, Salt_Peter <pj*****@yahoo.comsaid:
On Jan 8, 2:20 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.comsaid:
>>On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
wrote:
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+
+

Funny thing, many compilers treat .C files as C++.

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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 8 '08 #6

P: n/a
Montezuma's Daughter wrote:
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.

--
Jim Langston
ta*******@rocketmail.com
Jan 8 '08 #7

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrites:
Montezuma's Daughter wrote:
>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.
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.

--
__Pascal Bourguignon__ http://www.informatimago.com/
You're always typing.
Well, let's see you ignore my
sitting on your hands.
Jan 8 '08 #8

P: n/a
Pete Becker wrote:
>>>>why can't everything be written in C file?
We don't disscuss C files, source files are typically *.cpp files in C+
Funny thing, many compilers treat .C files as C++.
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.
He got you there, Salt_Peter :)
Jan 9 '08 #9

P: n/a
On Jan 8, 8:20 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.comsaid:
On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
wrote:
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++
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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 9 '08 #10

P: n/a
On Jan 8, 10:34 pm, Pascal Bourguignon <p...@informatimago.comwrote:
"Jim Langston" <tazmas...@rocketmail.comwrites:
Montezuma's Daughter wrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 9 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.