I'll answer the last question first to set some definitions:
Are common functions like printf or scanf a part of the runtime or the C library ? What kind of functions would be provided by the runtime?
If you follow this definition of runtime, then yes: it treats "runtime" as nearly synonymous with "standard library implementation" (or at least the compiled part of that). But that's not all there is to it.
Aside from the library, there's also something called the "start file" in common C implementations and this may also be called "the runtime" (e.g. GCC's crt1.o
). This little object does not typically export any public functions.
So what exactly does the runtime do?
Aside from providing the standard APIs (if we use the broad definition of runtime that includes the library), it contains an entry point (called _start
on Linux) for the program that conforms to OS conventions, and this entry point
- calls into
main
using C conventions (with argc
and argv
);
- sets up the environment for
getenv
;
- transfers the return codes from
main
or the argument to exit
back to the operating system;
- makes sure
atexit
callbacks are run even when main
returns;
- initializes data structures for
malloc
;
- initializes the standard streams
stdin
, stdout
, stderr
;
- may load shared libraries such as
libc
and whatever -l
flags for shared libs where given at compile time.
Does it only act as this interface (so that if we implement it ourselves, we don't need the runtime), or it has some other functions as well?
That's really platform-dependent. The runtime will do all that is needed to give C programs the environment that the C standard and the compiler manual guarantee. E.g. in the bad old days of MS-DOS, runtimes called "DOS extenders" would also set the CPU in protected mode and do all kinds of API translation to call into DOS in this mode.
Does the runtime link in any files when a program is compiled?
The runtime is at work at runtime (duh), not compile time. If you mean, does the runtime link in files at runtime: it may, e.g. it might link in the DLLs that an executable needs. If you mean, does the compiler link in the runtime files: typically yes, but it may have a flag to disable this or pick different runtimes.