By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

scanf address

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ad************@gmail.com (aditya) writes:
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.
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
Nov 14 '05 #2

P: n/a

"aditya" <ad************@gmail.com> wrote

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.
Nov 14 '05 #3

P: n/a
aditya wrote:
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.
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.