Does compiler provide these functions: printf, scanf? - c

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

Related

Is stdio.h a library?

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.

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.

Cannot use standard library function name for a global variable, even when no headers are included

Declaring a global variable with the same name as a standard function produces an error in clang (but not gcc). It is not due to a previous declaration in a header file. I can get the error by compiling the following one-line file:
extern void *memcpy[];
Clang says
foo.c:1:14: error: redefinition of 'memcpy' as different kind of symbol
foo.c:1:14: note: previous definition is here
Apparently this only happens for a few standard functions. printf produces an error, fprintf produces a warning, fseek just works.
Why is this an error? Is there a way to work around it?
Motivation. I am using the C compiler as a compiler backend. C code is programmatically generated. The generated code relies on byte-level address arithmetic and pointer type casting. All external symbols are declared as extern void *variablename[];.
According to the C standard (ISO 9899:1999 section 7.1.3), "all external identifiers defined by the library are reserved in a hosted environment. This means, in effect, that no user-supplied external names may match library names."
Your problem can be easily solved by adding a unique prefix to all your identifiers, e.g. "mylang_".
As an alternative, you can avoid the problem by using the LLVM or GCC -ffreestanding flag, which will compile your code for a non-hosted environment. (The C standard specifies that the restriction only applies to a hosted environment.) In this case you can use all the names you want (apart from main, which is still your program's entry point), but you must make your own arrangements for your library. This is how operating system kernels can legally define their own versions of the C library functions.
The reason is explained here and a relevant extract is given below. http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html
I get an error in gcc as well.
The names of all library types, macros, variables and functions that come from the ISO C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:
Other people reading your code could get very confused if you were using a function named exit to do something completely different from what the standard exit function does, for example. Preventing this situation helps to make your programs easier to understand and contributes to modularity and maintainability.
It avoids the possibility of a user accidentally redefining a library function that is called by other library functions. If redefinition were allowed, those other functions would not work properly.
It allows the compiler to do whatever special optimizations it pleases on calls to these functions, without the possibility that they may have been redefined by the user. Some library facilities, such as those for dealing with variadic arguments (see Variadic Functions) and non-local exits (see Non-Local Exits), actually require a considerable amount of cooperation on the part of the C compiler, and with respect to the implementation, it might be easier for the compiler to treat these as built-in parts of the language.
The page also describes other restricted names.

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

Problems with different IDEs for C programming

I'm learning C for 2 months. I experimented with different IDEs and my experiments resulted in confusion. Because for e.g. in NETBEANS I can use abs function without stdlib.h library, but when I tried to do the same thing in Visual Studio 2012 it gave a an error. Or a very odd thing in NETBEANS I can use functions from math.h library without declaring the library. Why is this happening? Can someone help? NETBEANS USES cygwin compilers.
In C you don't need to include the headers in order to use the functions. Older compilers don't always warn about that though. Also, different compilers might provide those functions in different ways; on some, they're not functions but macros. With macros, you need to include the headers.
It's good practice to always include the headers that provide the functions you need, so that you get the function prototypes. That's the only way the compiler can check for errors (correct types of passed function arguments, for example.) If you call a function for which you have no prototype, you get an implicit declaration of that function. That means the compiler just takes a guess and hopes you're using the function correctly, but has no way to check. That's why this won't work with macros, since a macro can't have a function declaration (implicit or not.)
The reason Visual Studio gives an error is because it's a C++ compiler, not a C compiler. C++ is a bit different from C. One of the differences is that C++ does not allow implicit function declarations. If you don't declare the functions you use (by including their header file in this case), then that's considered an error. C++ is mostly compatible with C, but that happens to be one of the few differences.
Btw, they're not libraries. They're header files. There's a difference. You have several standard headers you can include, but you only have one library; the C library. On most systems, you also have a math library, which only contains math functions. The point though is that several header files can be (and usually are) part of the same library.
my experience with C has been the same. different compilers has different libraries and sometimes they don't stick to the standards.
some compiler vendors try to lock you in (XXXXX$XXX) :)

Resources