9.1. IntroductionThere is no doubt that the Standard Committee's decision to define a set of library routines will prove to be a huge benefit to users of C. Previously there were no standard, accepted, definitions of library routines to provide support for the language. As a result, portability suffered seriously. The library routines do not have to be present; they will only be present in a hosted environment—typically the case for applications programmers. Writers of embedded systems and the writers of the hosted environment libraries will not have the libraries present. They are using ‘raw’ C, in a freestanding environment, and this chapter will not be of much interest to them. The descriptions (except for this introduction) are not meant to be read as a whole chapter, but as individual pieces. The material included here is meant more for information and convenient reference than as a full tutorial introduction. It would take a full book by itself to do real justice to the libraries. 9.1.1. Headers and standard typesA number of types and macros are used widely by the library
functions. Where necessary, they are defined in the
appropriate
The Standard isn't quite as restrictive about identifiers as the list above is, but it's a brave move to make use of the loopholes. Play safe instead. The Standard headers are: <assert.h> <locale.h> <stddef.h> <ctype.h> <math.h> <stdio.h> <errno.h> <setjmp.h> <stdlib.h> <float.h> <signal.h> <string.h> <limits.h> <stdarg.h> <time.h> A last general point is that many of the library routines
may be implemented as macros, provided that there will be no
problems to do with side-effects (as Chapter 7 describes).
The Standard guarantees that, if a function is normally
implemented as a macro, there will also be a true function
provided to do the same job. To use the real function,
either undefine the macro name with
some function("Might be a macro\n");
(some function)("Can't be a macro\n");
9.1.2. Character set and cultural dependenciesThe Committee has introduced features that attempt to cater for the use of C in environments which are not based on the character set of US ASCII and where there are cultural dependencies such as the use of comma or full stop to indicate the decimal point. Facilities have been provided (see Section 9.4) for setting a program's idea of its locale, which is used to control the behaviour of the library functions. Providing full support for different native languages and customs is a difficult and poorly understood task; the facilities provided by the C library are only a first step on the road to a full solution. In several places the ‘C locale’ is referred to. This is the only locale defined by the Standard and effectively provides support for the way that Old C worked. Other locale settings may provide different behaviour in implementation-defined ways. 9.1.3. The <stddef.h> HeaderThere are a small number of types and macros, found in
Subtracting one pointer from another gives a result whose
type differs between different implementations. To allow
safe use of the difference, the type is defined in
For reasons which still escape us, there is an ‘implementation
defined null pointer constant’ defined in
#include <stdio.h>
#include <stddef.h>
FILE *fp;
if((fp = fopen("somefile", "r")) != NULL){
/* and so on */
There is also a macro called
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
main(){
size_t distance;
struct x{
int a, b, c;
}s_tr;
distance = offsetof(s_tr, c);
printf("Offset of x.c is %lu bytes\n",
(unsigned long)distance);
exit(EXIT_SUCCESS);
}Example 9.1The expression Note carefully the way that a The last item declared in 9.1.4. The <errno.h> HeaderThis header defines errno along with the macros
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
errno = 0;
if(some_library_function(arguments) < 0){
/* error processing code... */
/* may use value of errno directly */
The implementation of What's more, you should only check Other library functions are free to set it to arbitrary values after a call unless their description explicitly states what they do with it. |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |