From: Montezuma's Daughter on
I was wondering why h file is needed?
I heard it is because in h file the compiler discover what memory is
needed for allocation.
is that true?
thank
From: Francis Glassborow on
Montezuma's Daughter wrote:
> I was wondering why h file is needed?
> I heard it is because in h file the compiler discover what memory is
> needed for allocation.
> is that true?
> thank

This question is so general that it is hard to answer. In general a
header file is needed so that the compiler knows the declarations of
names that you are going to use. These names include function IDs. The
compiler also needs to know the definition of a class which you are
going to create instances of in order to allocate appropriate storage.
From: Philip Potter on
Montezuma's Daughter wrote:
> I was wondering why h file is needed?
> I heard it is because in h file the compiler discover what memory is
> needed for allocation.
> is that true?
> thank

A C .h file, in general, contains the following:

1) function declarations
2) macros (#defines)
3) definitions of new types

and, less commonly,

4) file-scope (global) variable declarations

These all do different things and serve different purposes:

1) function declarations
Function declarations tell the compiler which functions a .c file will
use but not declare. For example, stdio.h will contain:
int fgetc(FILE *stream);
or something that means the same thing.

This tells the compiler that there exists a function called fgetc which
takes a FILE * parameter and returns an int. From that point on, the
compiler knows what to do when it encounters "fgetc" in code. Without
the declaration, a call to "fgetc" is unlikely to succeed.

2) macros (#defines)

These define constants and preprocessor macros. A common one to see
defined is
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
For more information about macros, read your textbook.

3) definitions of new types

This includes typedefs, structs, unions, and (in C++ only) classes. For
example, stdio.h defines the type FILE.
For more information, read your textbook.

4) declarations of global variables

These are generally frowned upon, but errno is an example of such a
declaration in a header file.

The purpose of a header file is to provide the above functionality.
There's probably a few other things I've missed.
From: James Dennett on
Montezuma's Daughter wrote:
> I was wondering why h file is needed?
> I heard it is because in h file the compiler discover what memory is
> needed for allocation.
> is that true?
> thank

Header files provide the interfaces which allow multiple separate
source files (more formally, multiple different translation units)
to be compiled separately and later linked together with some kind
of safety.

Other languages have other ways of achieving somewhat similar
results; some do it well (with separate implementation and
definitions for modules/classes/components) and some don't.

-- James
From: asm23 on
Philip Potter wrote:
> Montezuma's Daughter wrote:
>> I was wondering why h file is needed?
>> I heard it is because in h file the compiler discover what memory is
>> needed for allocation.
>> is that true?
>> thank
>
> A C .h file, in general, contains the following:
>
> 1) function declarations
> 2) macros (#defines)
> 3) definitions of new types
>
> and, less commonly,
>
> 4) file-scope (global) variable declarations
>
> These all do different things and serve different purposes:
>
> 1) function declarations
> Function declarations tell the compiler which functions a .c file will
> use but not declare. For example, stdio.h will contain:
> int fgetc(FILE *stream);
> or something that means the same thing.
>
> This tells the compiler that there exists a function called fgetc which
> takes a FILE * parameter and returns an int. From that point on, the
> compiler knows what to do when it encounters "fgetc" in code. Without
> the declaration, a call to "fgetc" is unlikely to succeed.
>
> 2) macros (#defines)
>
> These define constants and preprocessor macros. A common one to see
> defined is
> #define MAX(a,b) (((a)>(b)) ? (a) : (b))
> For more information about macros, read your textbook.
>
> 3) definitions of new types
>
> This includes typedefs, structs, unions, and (in C++ only) classes. For
> example, stdio.h defines the type FILE.
> For more information, read your textbook.
>
> 4) declarations of global variables
>
> These are generally frowned upon, but errno is an example of such a
> declaration in a header file.
>
> The purpose of a header file is to provide the above functionality.
> There's probably a few other things I've missed.
Great answer! Though I don't know these knowledge till now, I have be
using c/c++ for 6 years.^_^