Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does compiler provide these functions: printf, scanf?

Tags:

c

I'm studying C language nowadays. In this book, it is said that the "compiler provides these library functions: 'printf','scanf'…".

I can't understand. Those functions are defined in the header file <stdio.h> aren't they?

Why does this book explain those functions are provided by the compiler?

like image 492
nujabes Avatar asked Dec 08 '22 02:12

nujabes


2 Answers

printf, scanf, and the other standard library functions are provided as part of the implementation.

A C implementation is made up of several components. The compiler is just one of them. The library is another; it consists of headers (commonly provided as source files like stdio.h) and some form of object code files containing the code that actually implements the library functions.

The header stdio.h only declares these functions; it doesn't define them. The declaration of printf is something like:

int printf(const char *format, ...);

The definition of printf is the code that actually does the job of parsing the format string, accessing the arguments, and sending the formatted output to stdout. That's typically (but not necessarily) written in C and provided as some kind of linkable object code.

For some C implementations, the compiler and the library are provided by the same organization. For others, they might be provided separately (for example MinGW combines the gcc compiler with Microsoft's library).

like image 116
Keith Thompson Avatar answered Dec 31 '22 09:12

Keith Thompson


The functions are provided by the standard library, which is a collection of precompiled code that is typically written by the compiler authors (but it is indeed not a part of the compiler itself).

Note, though, that the functions are only declared in the header files. The definition resides in source files that have already been compiled.

like image 23
Aasmund Eldhuset Avatar answered Dec 31 '22 09:12

Aasmund Eldhuset