473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

why is h file needed?

Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks
Jan 8 '08 #1
10 2009
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
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
12
by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar, without having to refresh or go to a seperate page,...
4
by: max | last post by:
Hello, Based on various posts, I've come up with the following code to load data into a js array dynamically without a screen refresh. It works, but you have to push the button twice before it...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
8
by: andrew.jefferies | last post by:
Hi, I'm trying to write a simple log parsing program. I noticed that it isn't reading my log file to the end. My log is around 200,000 lines but it is stopping at line 26,428. I checked that...
1
by: gamernaveen | last post by:
Hey guys , am just getting into the AJAX scene and am a noob. I am really worried about Ajax upload , cant figure it out. I have a basic html form , like this <form action="upload.php"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.