header file in C

C

candy

hi all,
I just want to know that whether the C header files( like stdio.h,etc

which the compiler provides) just contains the function declarations
or they also contain some additionalinformation like where to look in

the memory for the defintions of the standard functions like

scanf(char*c,...)).

Also,if there is no information about the location of the memory where
the definitions of the standard functions is present,then can we just

declare the function which we want to use,ourselves,and then don't

include the required header file/s.

Also,can you please tell me the steps in brief through which a typical

complier passes the source file so as to generate the final executable

file.

Also,can you please tell me that why an executable file made in Windows

operating system is not recognised by the Linux operating System and

vice-versa.

Thanks in advance,
Candice.
 
J

Joona I Palaste

candy said:
hi all,
I just want to know that whether the C header files( like stdio.h,etc
which the compiler provides) just contains the function declarations

That's right, except that they may also contain type declarations and
#defines.
or they also contain some additionalinformation like where to look in
the memory for the defintions of the standard functions like
scanf(char*c,...)).

No. The C language does not define any such facility.
Also,if there is no information about the location of the memory where
the definitions of the standard functions is present,then can we just
declare the function which we want to use,ourselves,and then don't
include the required header file/s.

That is correct. All the compiler and linker care about is getting the
needed declarations correctly. They don't care where they come from.
Also,can you please tell me the steps in brief through which a typical
complier passes the source file so as to generate the final executable

This is too advanced a topic to cover briefly here.
Also,can you please tell me that why an executable file made in Windows
operating system is not recognised by the Linux operating System and
vice-versa.

Because they use different object code models.
 
M

Minti

candy said:
hi all,
I just want to know that whether the C header files( like stdio.h,etc

which the compiler provides) just contains the function declarations
or they also contain some additionalinformation like where to look in

the memory for the defintions of the standard functions like

scanf(char*c,...)).

Also,if there is no information about the location of the memory where
the definitions of the standard functions is present,then can we just

declare the function which we want to use,ourselves,and then don't

include the required header file/s.

Also,can you please tell me the steps in brief through which a typical

complier passes the source file so as to generate the final executable

file.

Also,can you please tell me that why an executable file made in Windows

operating system is not recognised by the Linux operating System and

vice-versa.


Go to a near bookstore or library and shout for "Compiler Principles, Tool
and Techniques" By Aho, Sethi and Ullman.
 
E

Eric Sosman

Joona said:
That's right, except that they may also contain type declarations and
#defines.


No. The C language does not define any such facility.

Joona is correct when he says the C language as such
has no way to place a particular function at a particular
memory location. However, each implementation of C uses
some amount of "implementation magic" outside the realm of
C itself, and it is at least conceivable that some "magic"
in some implementation might locate its library functions
this way. It would certainly be an unusual approach, but
it's within the realm of possibility. Systems have used
things like "jump tables" or "branch vectors" before, and
perhaps some might still use the technique for C.
That is correct. All the compiler and linker care about is getting the
needed declarations correctly. They don't care where they come from.

Joona is right again, but some library functions cannot
be declared "free-hand" because there's no way to write their
prototypes without using the contents of the relevant headers.
Take fprintf(), for example: you could write

int fprintf(FILE*, const char*, ...);

easily enough, but you need to have `FILE' declared before
you can do this. The *only* correct way to declare `FILE'
is to include <stdio.h> (because the details of `FILE' vary
from one implementation to another), and since <stdio.h> also
declares fprintf() there's little reason to add your own,
redundant declaration.

Another example: you could write

size_t strlen(const char*);

but where is `size_t' declared? It turns out that many of
the standard header files declare `size_t', and that you could
in fact obtain its declaration by including <stdio.h> or
<stddef.h> or a few others, without including <string.h>.
But the fact that something *can* be done doesn't mean that
it *should* be done; in this case it doesn't even save you
any keystrokes to write a "free-hand" declaration.
 
M

Malcolm

candy said:
I just want to know that whether the C header files( like stdio.h,etc
which the compiler provides) just contains the function declarations
or they also contain some additionalinformation like where to look in
the memory for the defintions of the standard functions like
If you look at a standard header file by opening it in an editor, you'll
probably find something that looks superficially like gibberish, but when
you examine it closely is a regular C header.
However not all implementations are like this. The compiler isn't required
to have standard headers as actual files.
As for "finding the function in memory", all library or object file
functions need to be located somehow. The standard libary is no different.
scanf(char*c,...)).

Also,if there is no information about the location of the memory where
the definitions of the standard functions is present,then can we just
declare the function which we want to use,ourselves,and then don't
include the required header file/s.
This will probably work, and language lawyers will be able to tell you
whether or not behaviour is defined. However for practical purposes we never
do this. If scanf() is available then stdio.h will also be available, and so
is always included.
Also,can you please tell me the steps in brief through which a typical
complier passes the source file so as to generate the final executable
file.
It first runs the C preprocessor on the file. Then it compiles all the
function definitions to an intermediate format, known as object code, then
it invokes the linker to join the object files and library files into an
executable.
Also,can you please tell me that why an executable file made in Windows
operating system is not recognised by the Linux operating System and
vice-versa.
Through wickedness. The processors are the same, and so the machine code is
basically identical. However the operating system writers have decided to
make the minor loading and initialisation code incompatible, so programs
won't run on each other's platforms and lawsuits / competition is avoided.
 
B

bd

Joona said:
That's right, except that they may also contain type declarations and
#defines.




No. The C language does not define any such facility.

Why can't an implementation use some sort of implementation-specific
extension in its headers? Heck, glibc has inline assembly code in some of
its headers.
 
F

Flash Gordon

Why can't an implementation use some sort of implementation-specific
extension in its headers? Heck, glibc has inline assembly code in some
of its headers.

It can define such an extension, however you are allowed to provide the
function prototypes yourself for the functions (not recommended and in
at least some cases you still need to include headers to get the
relevant types) or in some cases it is valid to use the function without
a prototype in scope at all, so the system must cope if any such fancy
facilities have not been used. Therefor I can't think that an extension
specifying where to find the functions would be of much help to the
implementation.

The fact the implementation could use inline functions and inline
assembler in the headers when they are included (whilst providing some
other mechanism for when they are not included) is just another argument
for including the relevant headers rather than providing the prototype
yourself.
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top