10

The C Programming Language calls stdio.h a library. However, I am being told that it's a header file only meant for the compiler, which is true, and therefore it's not a library.

Other programming sites on the Internet call it a library. Is the definition of library different now?

Some C programs start with #include <stdio.h> because the C language does not include file manipulation functions.

Update with quote from The C Programming Language by Brian Kernighan and Dennis Ritchie, Second Edition, page 3 (Introduction):

A second significant contribution of the standard [ refers to "the ANSI standard, or "ANSI C", completed in 1988] is the definition of a library of accompany C. It specifies functions for accessing the operating system (for instance to read and write files), formatted input and output, memory allocation, string manipulation, and the like. ... Programs that use this library to interact with a host system are assured of compatible behavior. Most of the library is clostly modeled on the "standard I/O library" of the UNIX system. This library was described in the first edition ...

It would seem a logical conclusion that the definition of library has evolved/changed/updated...

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 2
    Get a better book? Don't believe everything you read on the internet... – Chris Dodd Aug 29 '19 at 22:23
  • 4
    @ChrisDodd "The C Programming Language" was written by an author of C. I don't know if there is a better book. On cursory glance through the book, I don't know if OP's statement is accurate. – ggorlen Aug 29 '19 at 22:24
  • 2
    OP, can you cite a page that you found this quote on? – ggorlen Aug 29 '19 at 22:27
  • I don't have the book at this office, but it's in the introduction. I looked it up this morning. The version I have has what feels like 3 introductions. It seems to be a later reprinting. – Nora McDougall-Collins Aug 29 '19 at 22:30
  • A minor correction, the book has 2 authors: Kerighan (sp?) and Ritchie. – Nora McDougall-Collins Aug 29 '19 at 22:32
  • You might want to double-check and verify that your memory is correct. I'm doing a text search through the book now for "library", and I don't see any indication in the first 150 pages to support your statement. I know Kernighan is a co-author, but he didn't write C, so I omitted him (although he's still an enormously credible source). – ggorlen Aug 29 '19 at 22:34
  • @ggorlen I would find it very hard to believe that the C bible depicts stdio.h as a "library". Most probably, it was something like this that was wrongly interpreted: `The first line of theprogram, #include , tells the compiler to include information about the standard input/output library;` (from chapter 1.1). – Marco Bonelli Aug 29 '19 at 22:35
  • So yes, OP, it would be helpful to add to edit your question adding a citation from the specific paragraph that made you ask this, if you can remember it. – Marco Bonelli Aug 29 '19 at 22:36
  • I have simply "include information about standard library". This doesn't say that the header file *is* a library, only that it includes information *about* the standard library. There are plenty of PDF versions of the book that can be pulled up with a quick Google search. It's lazy to definitively state that the book says something without a citation. – ggorlen Aug 29 '19 at 22:37
  • 1
    @ggorlen *The C Programming Language*, while indeed written by the author of C, predates the standardization of C in 1989, so it contains a number of inaccuracies when dealing with modern C. – PC Luddite Aug 29 '19 at 23:37
  • @PCLuddite That's inevitable for a book of its age, regardless of authorship, but I don't see any evidence presented that this is one such inaccuracy. The comment is mainly in response to "get a better book"--I'm not arguing that "The C Programming Language" has no inaccuracies, I'm arguing that it's one of the best C books available, so it's silly to be so dismissive of it over something that it *supposedly* stated (again, no evidence, no citation was presented). – ggorlen Aug 29 '19 at 23:55
  • At least the current [GNU glibc](https://sourceware.org/git/?p=glibc.git;a=tree;f=include;h=75713feb7ae64391e1515f943872bbb19085d08f;hb=refs/heads/master) has removed that language. (you can fine `stdio.h` -- and click `raw`) for the content. – David C. Rankin Aug 30 '19 at 00:00
  • @ggrorlen There are many books with titles that contain things like "the C programming language"; the OP does not reference a specific book. My comment "get a better book" was mainly a reference to the fact that there are many bad books out there. – Chris Dodd Aug 30 '19 at 00:00
  • 1
    @ChrisDodd The OP has clarified that it's *the* C Programming Language above, though, which may not have been apparent at first (I assumed it was). But we still don't have a quote, so it's all speculation... – ggorlen Aug 30 '19 at 00:07
  • A Facebook friend, who has the book handy, found this, which I believe is what I noted this morning (unfortunately my book is paper which is tied to a location where I will not be until later). I think I may have found the source of some of the confusion on page 6 of K&R2e: “The first line of the program, #include tells the compiler to include information about the standard input/output library;” – Nora McDougall-Collins Aug 30 '19 at 00:14
  • 1
    See my comment 7 remarks above. Saying we're "including information *about* the standard library" (emphasis mine) doesn't seem to equate to "calling `stdio.h` a library". Thanks for identifying the quote in question. I strongly recommend editing your post to include the quote itself, and stating that it is *your interpretation* that it calls stdio a standard library. Then, I believe the correct answer to your question is that this is a misinterpretation, which is Caleb's answer. – ggorlen Aug 30 '19 at 01:23
  • See quote added to original question – Nora McDougall-Collins Aug 30 '19 at 02:46

2 Answers2

12

No, stdio.h is not a library, it's a header file. A common mistake when approaching C is to call every header file a library, that's just wrong.

The C standard library is a collection of functions, which are declared in header files, and stdio.h is one of them. The name stands for "Standard Input Output", so in that file you can find all the declarations of functions that deal with input, output and files. You can find a list of the header files included in the C standard library here.

A library is a compiled binary file (or in general, a collection of binary files), which can be linked when compiling a program to take advantage of the functions made available by the library (i.e. exported). Header files are then used to identify the names and the signatures of those functions, so that the compiler knows how to call them.

Commonly, for small libraries, a single header file is enough, so that's easy for beginners to confuse an header file with the library itself. The C standard library however is very complex and has a lot of functions, so they are declared in different header files.

C programs start with #include <stdio.h> because the C language does not include file manipulation functions.

Yes, that's right. The C specification only concerns the language itself (syntax, types, etc), and does not define any "standard" function.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
5

My book, "The C Programming Language" calls stdio.h a library. Now, I am being told that it's a "header file" only meant for the compiler, which is true, and therefore it's not a library.

I have a copy of that book (the first edition, and also the ANSI edition), and I don't remember there being any confusion about the difference between a header file and a library. Can you point us to where you're looking? On page 152, for example, I see:

Each source file that refers to an input/output library function must contain the line
#include <stdio.h>

And that's true enough... it's not saying that stdio.h is a library, but rather that you have to include the header file if you want to use the library. Similarly on page 176:

The data structure that describes a file is contained in , which must be included (by #include) in any source file that uses routines from the standard input/output library. It is also included by functions in that library...

In this paragraph that library refers to "the standard input/output library," not stdio.h itself. I could see how someone might misread that, but in context the book really isn't calling stdio.h a library here.

If you can point us to the particular passage that you're looking at, perhaps we can explain better.


Update:

The passage you quote from the book is from the introduction to the second edition, and it's talking about the history of the language up to that point (the second edition came out in 1988). In particular, the paragraph is talking about the C standard library:

A second significant contribution of the standard is the definition of a library to accompany C...

It looks like the part that inspired your question is this:

...Most of the library is closely modeled on the "standard 1/0 library" of the UNIX system. This library was described in the first edition, and has been widely used on other systems as well...

In all cases, when the text says library it really means that, not header file. Every C library has one or more associated header files that provide the interface to the associated library; without the header(s), you (and your compiler) wouldn't know how to access the functions that are defined in the library. For example, the fopen() function is declared in the stdio.h with a function prototype:

FILE *
fopen(const char * restrict path, const char * restrict mode);

Once you have the declaration of fopen() available, the compiler knows how to generate instructions to call that function, and the linker will connect your code to the actual function definition in the library file itself. So when the text says standard I/O library, it's really talking about the library, and the header file of the same name is just an ancillary file that lets you access the library.

It would seem a logical conclusion that the definition of library has evolved/changed/updated...

No, that's not what's happened; header files and libraries are and have always been distinct but related things, and the meaning really hasn't changed since the book was written, at least with respect to C.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    Eh.. for a beginner, the phrase *"Each source file that refers to an input/output library"* immediately followed by `#include ` could be misinterpreted as `stdio.h` being that "input/output library". It's not that unimaginable. That's probably what happened IMHO. – Marco Bonelli Aug 29 '19 at 22:45
  • See quote added to original post. – Nora McDougall-Collins Aug 30 '19 at 02:47
  • It seems that a quick answer is that stdio.h is how C connects to the library. – Nora McDougall-Collins Aug 30 '19 at 13:48
  • 1
    @NoraMcDougall-Collins That's about right. Header files are very common in C programming... As you write more complex programs, you'll eventually break those programs into multiple `.c` source files, and each one will have a `.h` header file to go with it. Including those header files is what tells the compiler how call the functions defined in another source file. Libraries are like source files that have already been compiled for you. – Caleb Aug 30 '19 at 14:04
  • 1
    @NoraMcDougall-Collins In any case, don't worry too much if it all seems a bit unclear right now... Even though *The C Programming Language* is pretty old as programming language books go, it's still a great book to learn from and an excellent reference. Keep asking when you need help with something. You'll get there. – Caleb Aug 30 '19 at 14:17