Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is stdio.h a library?

Tags:

c

libraries

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

like image 435
Nora McDougall-Collins Avatar asked Aug 29 '19 22:08

Nora McDougall-Collins


People also ask

Is Stdio a HA package?

stdio. h is in the glibc package.

Which library is Stdio in?

C Library - <stdio.

Is stdio.h library in C++?

h> is a statement which tells the compiler to insert the contents of stdio at that particular place. In C/C++ we use some functions like printf(), scanf(), cout,cin. These functions are not defined by the programmer. These functions are already defined inside the language library.


2 Answers

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.

like image 142
Marco Bonelli Avatar answered Oct 23 '22 19:10

Marco Bonelli


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.

like image 27
Caleb Avatar answered Oct 23 '22 17:10

Caleb