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.
really. You could then just define the compilation units (or