-1

I am currently learning C. I understand that many common functions, like printf and scanf are not actually part of the C language -- they are part of the "standard library" of functions.

My question is, why aren't such functions built into the language? Is it a philosophical/design consideration? A matter of efficiency when compiling programs? A necessity in order to act as a "middle layer" to ensure compatibility with different operating systems? Something else entirely?

chopper
  • 6,649
  • 7
  • 36
  • 53
intelli78
  • 137
  • 1
  • 8
  • Doubtful this has a real answer because it will be just speculation and/or opinion. Saying that the only "real answer" can be is "Because", because they wanted to do it that way so they did. Was it wise or unwise? Was it wise or unwise that other languages do? – old_timer Oct 13 '14 at 18:26
  • What do you mean with "are not actually part of C"? – dyp Oct 13 '14 at 18:26
  • 5
    `printf` and `scanf` _are_ part of C. There is the "C Language" and "C Standard Library". These are both part of the "C Standard" if that makes more sense to you. – Cory Nelson Oct 13 '14 at 18:26
  • try programmers.stackexchange.com with this question. – old_timer Oct 13 '14 at 18:27
  • Some of the C Standard library functions are implemented using compiler intrinsics; for example `memcpy` in many implementations. – dyp Oct 13 '14 at 18:28
  • 2
    I think you're going to need to clarify this question. They are, by some lights "built in" to the language (e.g. printf appears in the C standard, and the C library does not itself have a separate document). Are you asking why the standard uses the terminology it does? Why compilers typically don't implement these functions as built-ins? – Logan Capaldo Oct 13 '14 at 18:28
  • 1
    There are free-standing implementations for C, which don't need to provide any of the `` functions. The C standard addresses them as well, so there's a "core language" and a library hosted implementations have to provide. – mafso Oct 13 '14 at 18:29
  • 1
    @dyp K&R C, 2nd ed., p.151: "Input and output facilities are not part of the C language itself ... In this chapter we will describe the standard library, a set of functions that provide input and output, string handling, storage management, mathematical routines, and a variety of other services for C programs." – intelli78 Oct 13 '14 at 18:34
  • @intelli78 Well, in that case I have to ask what K&R meant with that statement. Maybe that the library can be described/specified/implemented by using the core language? – dyp Oct 13 '14 at 18:40

3 Answers3

7

They are part of C. A C implementation consists of a compiler and a library (and other components, like a linker).

The C core language includes facilities that make it possible to write library code that can be used by other programs. The standard library portion of the standard specifies a library that can be implemented using the facilities defined in the core language.

Some languages do have things like a print command built into the language. C's facilities for writing and invoking library code written in C are powerful enough that that's not necessary.

Furthermore, most of the library is optional for "freestanding" implementations (mostly for embedded systems). There are implementations that support the full core language but that don't provide most of the C standard library.

And a compiler and library can be provided separately. For example, gcc is a compiler; it's commonly used with different library implementations on different systems (GNU libc on Linux, "newlib" on Cygwin, the Microsoft library on Windows with MinGW, and so forth). Mixing and matching like that would be much more difficult if the library were integrated into the core language.

The C language standard (the link is to the newest freely available draft) defines C. Section 6 defines the core language; section 7 defines the standard library.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

The thing is that standard allows two kinds of conforming implementation: hosted and freestading, see N1570 4/p6:

The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program that does not use complex types and in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>. A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.

By such design, where the libraries are organized as standard headers it's convienent to simply "cut-off" some header if it is not supported.

Note that C standard defines all headers along with function for standard C library. It's included in C standard indeed, not separate thing somewhere else.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
2

They're part of the language, they're just not part of the grammar.

Factoring I/O out into a separate function library buys you the following:

  1. The grammar becomes easier to parse;
  2. You can target a wider range of platforms; any I/O foibles are handled by the library functions, not by hacking the code generator;
  3. You can implement only as much as is needed to support the platform (i.e., an embedded controller probably doesn't need to read or write formatted output);
John Bode
  • 119,563
  • 19
  • 122
  • 198