4

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
nujabes
  • 891
  • 2
  • 8
  • 17

5 Answers5

6

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).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Then, "the compiler provides library functions" is wrong statement? – nujabes Aug 14 '15 at 00:45
  • Are they separated thing, right? I mean, 'C library functions' , 'compiler'. – nujabes Aug 14 '15 at 00:47
  • A complete implementation for C provides a compiler and the runtime support needed. On many Unix systems, the C runtime library is somewhat separate from the compilers, but the compiler must know how to create programs (otherwise it isn't much use) and must know which library it is going to use. But the compiler is very definitely a wholly separate program from either the programs you create with it or from the 'default C library' (which isn't even a program — it is just a library). – Jonathan Leffler Aug 14 '15 at 00:48
  • @JonathanLeffler: The compiler doesn't necessarily have to know which library the program will use; that's up to the linker. – Keith Thompson Aug 14 '15 at 00:51
  • And then you get into questions of 'what is the compiler' — yes, you're right, and yes (IMO, of course) I'm also right, but we're using different definitions of the term 'compiler' which is why we're both right for our different definitions. – Jonathan Leffler Aug 14 '15 at 00:52
5

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Um... sorry I don't understand. If the functions are declared in the header files, why is it needed to provide those functions from standard library again? – nujabes Aug 14 '15 at 00:33
  • 4
    @nujabes: the header files only contain the *prototypes*, which is all that is needed your file to compile; the actual implementation is usually provided already compiled in some static or dynamic library. This avoids the need to recompile the whole standard library each time you build your program. – Matteo Italia Aug 14 '15 at 00:38
  • 2
    @nujabes: Because the header file does not contain the code that implements the functions. The implementation object files are in the standard library; the header simply declares the functions that are defined for you to use. – Jonathan Leffler Aug 14 '15 at 00:39
  • @JonathanLeffler Oh Thank you , that makes sense! – nujabes Aug 14 '15 at 00:46
  • @MatteoItalia Thank you! then "Compiler provides library functions" statement is wrong, right? Summary: Library function is just library function. The compiler just translate those function. Is it true ? – nujabes Aug 14 '15 at 00:49
1

By saying, "Compiler provides these library functions , 'printf','scanf'..", the author of the book is being sloppy.

A standard conforming C implementation provides declarations of those functions in header files and implementations of those functions in some short of library. A compiler is just one aspect of a C programming environment.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you ! You said "some short of library" , is it standard library ? – nujabes Aug 14 '15 at 00:35
  • 1
    @nujabes, different programming environments provide different ways of supporting the standard library. I didn't want to be very specific. – R Sahu Aug 14 '15 at 00:38
1

The compiler does not provide those functions. The goal of the compiler is to translate your high-level language code into another form, in particular an executable binary.

The standard C library contains the functions in stdio.h and stdlib.h.

The compiler links the standard library with your code so that your code can call those functions.

For almost all libraries, you have to tell the compiler what libraries you want to link. It so happens that for some compilers, the library (libc) for stdio.h and stlib.h is automatically linked without you needing to specify them.

Community
  • 1
  • 1
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • The default C library normally supplies all the C functions from the Standard C library except (perhaps — it depends on the platform) the maths functions from ``. (I've never needed to use ``; I'm not sure whether they go with the the maths functions or with the other functions.) When the maths library is separate, it is normally loaded with `-lm`, and the name is `libm.a` or something similar – but library names and extensions vary by platform. Mac OS X has the maths functions in the main library. _[…continued…]_ – Jonathan Leffler Aug 14 '15 at 00:46
  • _[…continuation…]_ I don't much like terminology that talks about 'the stdio and stdlib libraries'. There are headers `` and ``, but there's only one library (or two libraries) — the C library (and perhaps the maths library). – Jonathan Leffler Aug 14 '15 at 00:46
  • @JonathanLeffler: thanks. I changed the wording of "the stdio and stdlib libraries" to be "the library (libc) for stdio.h and stlib.h". – stackoverflowuser2010 Aug 14 '15 at 00:54
1

Those functions provided by standard library and GCC includes built-in versions of many of the functions in the standard C library. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

Wei Shao
  • 51
  • 3