Is stdio.h a library? - c

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

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.

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.

Related

Procedure to include own functions to ANSI C Standard library

How one can include his/her own programming functions to standard C (ANSI C) library? And any one who is learning or working on C language able to use those functions anywhere anytime, no need of development in general.
Example : someone developed function named "FunFun()" and assume it does fantastic work for most programmers. so how anyone can access this "FunFun" function without developing and just including standard library?
The sane way to approach it would be to develop a 3rd party library and make it available over the internet through open source, Github etc.
The GNU C dialect is one such example, which is a collection of non-standard compiler extensions used by the GCC compiler. One could join the GCC open source group and try to get the new function added there. It would still not be standard library C, but the GCC extensions are often an inspiration to the C standard and several of them (designated initializers, flexible array members, anonymous struct/union etc) have been adopted into the language itself with the C99 and C11 standards. One of the purposes for GNU C is actually to serve as an experimental playground where new languages features can be tried out live.
If you truly wish to add a new function to the actual C standard library, you would have to join the ISO working group and convince them that the function should be added to the language. Or alternatively find a member of the committee and convince them to speak in favour of the new function.
All this of course assuming you are a C programming veteran, or otherwise nobody will likely take you seriously.
Your question can't be answered because it's based on several wrong assumptions.
Things like stdlib.h are not libraries. They are header files intended to be included in your program. Including means the contents are literally pasted into your program at the point of inclusion before the actual compilation happens. They are typically used for declaring functions, types, global variables etc a library provides. The actual library is then linked against after compilation.
There's no such thing as the C library as well as there's no such thing as the C compiler. c is a language that is specified in an open standard (if you're interested, here's the last draft of the latest standard version C11). There are many actual implementations and a complete implementation consists of at least a compiler and a standard library. You can of course implement your own standard library. It's a lot of work to have it really conform to the standard (you would have to implement printf() and scanf() correctly, for example). With your own standard library, you can also include your own extensions, but this would only mean people using your standard library (instead of e.g. glibc on a GNU system) could directly use them.
For having a function available on any implementation of C, it would be necessary to have the C Standard specify it. You won't get a new function in the standard without some very good reasoning.
So if you want to make your own function available to others, do what everyone does and just implement it in your own library. Users can download it, include its headers and link against it.

Does compiler provide these functions: printf, scanf?

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

How to know, what is inside a header file?

I wonder what is inside stdio.h and conio.h etc.
I want to know how printf and scanf are are defined.
Is there a way I can open stdio.h and see what is written inside?
Depending on your implementation, you should be able to open any .h file in your favorite editor and read it directly; they're (usually) just plain text files.
However, stdio.h will only give you the declarations for printf and scanf; it won't contain the source code for them. Most compilers don't ship the source code for standard library functions; instead, they ship precompiled libraries which are linked with your code when you build the executable.
If you're willing to spend some money, P.J Plauger's The Standard C Library is a good resource that shows an implementation of the standard library functions.
When the preprocessor includes a header file into a source file, that inclusion is very much literal. That means that the header files are normal text files with source in them, and must be readable by the compiler (and therefore by you). You just have to find where they are, and you can open them like any other text file.
However, you won't find out how functions are defined, just how they are declared. And some structures are supposed to be "black boxes", whose data members should be considered private. Usually the source for the standard C library is available or downloadable, so try and find that too. It all depends on what compiler you're using.
You might also want to check out a reference site such as this one. There you can find pretty detailed information about e.g. printf.
Those headers generally chain include more machine/OS specific headers.
If you are on Linux/OS X then you can get some more info with
man stdio
Also check out http://www.cplusplus.com/reference/cstdio/ https://en.wikipedia.org/wiki/Conio.h
Most compilers allow you to read the results after the preprocessor (the compilation step that processes the #include directives) has been run. With gcc for instance, use the -E command-line option.
You can always rely on the Internet's supply of Unix-style manual pages, by searching for "man something" you can look for the relevant manual section for something.
For instance, there are pages for both printf() and scanf().
You can easily see there that the declarations aren't very special, and quite obvious from the usage. It's just int printf(const char *format, ...); for instance.
the content of some headers is defined by the C-Standard.
other headers are defined by the library that provides it.
Some headers are defined from the system for that you are writing the code (may fall into the second case since the OS provides the libs)
depending on that you may look into c language reference or you may look into the libraries manual or in the OS's API reference.
But one thin is for sure. if you can include a header (and the compiler does not complain that he could not find it) than you also can look into it. just look into the standard include directories of the compiler or the additional include directories that are specified in project file ore Makefile to find the files on your file system.
But usually the better way is to look in the Documentation because the header itself may be difficult to read because of many #ifdefs and further includes
The most fundamental way to find out what's inside those headers is to read them. Of course, you must locate them first. To this end you can use this short shell code:
gcc -E -M - << EOF
#include <stdio.h>
EOF
This will provide you with a complete list of all the headers directly or indirectly included by #include <stdio.h>. Of course, if you are only interested in the 'stdio.h' header itself, you can just do
locate stdio.h
but this will usually list quite a few false positives.

what is libc? what are the functions it includes? how can we get the source code of it?

As per Wikipedia there are many variants of standard C library based on operating system and compilers.
Ref: http://en.wikipedia.org/wiki/C_standard_library
But I want to understand that how plenty of functions which are declared in different headers(eg: stdio.h, string.h, stdlib.h etc. ) are defined in single library. Is the source code file is same for all these header files or there are different libraries for stdio.h, string.h etc? As I am beginner to programming I don't know if multiple source code files can generate a single library like executable. If it is possible then I can understand that libc contains definition of all the standard header files. Where can I see the source code of standard C library?
Is it a static library or dynamic library? If both versions are present in my environment(OS/IDE) which one get linked when I include any standard header file in my source code. Is it IDE dependent? But in case of gcc, programmer does not include libc explicitly.
Is libc a standard name for standard C library?
In windows operating system/environment is it already present or not? If it is present what is the name of it(is it libc only)?
Is there any other standard C library like libm?
Generally speaking, a header (.h) file contains the declarations of functions and variables. Implementation files (.c) contain the actual implementation of the declared functions. Since several implementation files can be translated and linked into a single library binary, you can have one library with multiple headers. Many C library implementations are Open Source, and you can look at their source code at their relative project pages. GNU libc and RedHat newlib are the most prominent. I am sure people will add more in comments.
Implementation defined. You can translate the very same sources into either a static or a dynamic library. It is not uncommon to have both versions installed on your system. Since virtually every executable requires libc, it is usually added to the linker input by default so you don't have to add -lc to every command line.
No. The standard name for the standard C library is "The Standard C Library". Note that virtually all implementations of the standard library extend the library with non-standard functions. These remain non-standard, even if they come as part of the standard library. (alloca() springs to mind.)
MSVCRT.dll or somesuch, if I remember correctly.
libm stands for the math section of the standard library, which is not added to the linker input by default as it is seldom required. There is only one standard C library, the one described by the ISO/IEC 9899 language standard (hence the name). There are many other libraries that can readily be assumed to be present on a given system, but only what's described in the ISO/IEC documents is "the standard".

seeing the header files in c in linux

i want to know how can one know the functions present in header files in c when running in linux systems
Here: http://www.gnu.org/s/libc/manual/html_node/index.html
If you are writing C on Linux, your standard C library is nearly always the GNU C library. All standard C functions are present in this library, and the link above is to extensive documentation for them.
If you are planning to use libraries other than the standard C library, you should find their documentation as well.
And don't forget, if you know the name of a function you can type man 3 funcname for help on that function, including the name of the header file it appears in. Use the 3 to specify the C Library Function section of the manual. If you forget it and there's a command-line program (section 1) or system call (section 2) with the same name, you'll get that page instead.
By looking at them. Header files are usually in /usr/include and you can easily browse that folder.
However, some functions might be in different header files depending on the system (linux/bsd/other unixes).

Resources