scanf address

A

aditya

hi all
can anybody please tell me that how a C compiler searches for the
defintion of the standard functions like scanf,printf,etc?

Does the compiler knows in advance the location in memory where the
definitions of these functions lie?If not,then what does it do to find
the defintions fo these functions?

Also can you please tell me that what is the difference between an
object file and an executable file.

Thanks in advance,

Aditya
 
B

Ben Pfaff

can anybody please tell me that how a C compiler searches for the
defintion of the standard functions like scanf,printf,etc?

Does the compiler knows in advance the location in memory where the
definitions of these functions lie?If not,then what does it do to find
the defintions fo these functions?

Also can you please tell me that what is the difference between an
object file and an executable file.

All of these questions are system-specific. You should ask them
in a newsgroup that talks about whatever system you're interested
in.
 
M

Malcolm

aditya said:
can anybody please tell me that how a C compiler searches for the
defintion of the standard functions like scanf,printf,etc?
Compilers vary. Normally the standard library is part of a library file,
which contains labels (like "scanf" or "printf") which the linker can use to
match the call in the source to the executable in the library. The
executable code is then extracted from the library, maybe modified so that
it runs from the right address, and added into the user's program.
However a modern system tends to be a lot more complicated, since function
calls may be resolved at runtime, for instance printf() may call one
function when invoked from a console, and another if the program is opened
by clicking on an icon.
Does the compiler knows in advance the location in memory where the
definitions of these functions lie?If not,then what does it do to find
the defintions fo these functions?
No, the compiler knows where they are in the library file, because the
library file contains a list of all the functions inside. When it gets a
name, it checks against the library list, and if there is a match, extracts
the function.
Also can you please tell me that what is the difference between an
object file and an executable file.
An object file is an intermediate file (not all compilers create them). It
is very similar to a library file, in that it contains largely-compiled
code, and a list of function names for linking. Sometimes the code will need
slight modificaton before including in the user's executable.
An executable file is a machine-code file. Normally all the labels and other
C-specific information will be stripped away, and it might not even be
possible to tell that the code has been compiled from C. Functions are
linked by jumps to raw addresses.
 
J

jacob navia

aditya said:
hi all
can anybody please tell me that how a C compiler searches for the
defintion of the standard functions like scanf,printf,etc?

Does the compiler knows in advance the location in memory where the
definitions of these functions lie?If not,then what does it do to find
the defintions fo these functions?

Also can you please tell me that what is the difference between an
object file and an executable file.

Thanks in advance,

Aditya

The compiler generates code. The code contains a call instruction to the
scanf function, and a record for the linker telling it that this is an
unresolved reference. That is all.

The linker sees this instructions in the object file generated by the
compiler, and searches for the function in its libraries list. When it
founds it, it inserts the machine code of the scanf function into the
growing executable.

The linker doesn't know where the code will be actually in memory when
is loaded. It just generates an executable file on the disk, that is
later loaded into RAM when the program is prepared for execution.

The loader reads the executable from disk, assigns a memory space for
it, and copies the file from disk into RAM, adjusting some parts of it
inserting where needed the adresses relative to the load address.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top