📄 lib_over.html
字号:
<HTML><HEAD><TITLE>C Library Overview</TITLE></HEAD><BODY><H1><A NAME="C Library Overview">C Library Overview</A></H1><HR><P><B><A HREF="#Using Standard C Headers">Using Standard C Headers</A>· <A HREF="#C Library Conventions">C Library Conventions</A>· <A HREF="#C Program Startup and Termination">Program Startup and Termination</A></B></P><HR><P>All Standard C library entities are declared or defined in one or more<B><A NAME="standard headers">standard headers</A></B>.To make use of a library entity in a program, write an<A HREF="preproc.html#include directive"><I>include</I> directive</A>that names the relevant<B><A NAME="standard header">standard header</A></B>.The full set of Standard C headers constitutes a<B><A NAME="hosted implementation">hosted implementation</A></B>:<CODE><A HREF="assert.html"><assert.h></A></CODE>,<CODE><A HREF="ctype.html"><ctype.h></A></CODE>,<CODE><A HREF="errno.html"><errno.h></A></CODE>,<CODE><A HREF="float.html"><float.h></A></CODE>,<CODE><A HREF="iso646.html"><iso646.h></A></CODE>,<CODE><A HREF="limits.html"><limits.h></A></CODE>,<CODE><A HREF="locale.html"><locale.h></A></CODE>,<CODE><A HREF="math.html"><math.h></A></CODE>,<CODE><A HREF="setjmp.html"><setjmp.h></A></CODE>,<CODE><A HREF="signal.html"><signal.h></A></CODE>,<CODE><A HREF="stdarg.html"><stdarg.h></A></CODE>,<CODE><A HREF="stddef.html"><stddef.h></A></CODE>,<CODE><A HREF="stdio.html"><stdio.h></A></CODE>,<CODE><A HREF="stdlib.html"><stdlib.h></A></CODE>,<CODE><A HREF="string.html"><string.h></A></CODE>,<CODE><A HREF="time.html"><time.h></A></CODE>,<CODE><A HREF="wchar.html"><wchar.h></A></CODE>, and<CODE><A HREF="wctype.html"><wctype.h></A></CODE>.</P><P>The headers<CODE><A HREF="iso646.html"><iso646.h></A></CODE>,<CODE><A HREF="wchar.html"><wchar.h></A></CODE>, and<CODE><A HREF="wctype.html"><wctype.h></A></CODE> are added with<B><A NAME="Amendment 1">Amendment 1</A></B>, an additionto the C Standard published in 1995.</P><P>Still more headers (not described here), and changes to existingheaders, are added with<B><A NAME="C99">C99</A></B>, a revisionto the C Standard published in 1999.</P><P>A <B><A NAME="freestanding implementation">freestandingimplementation</A></B>of Standard C provides only a subset of these standard headers:<CODE><A HREF="float.html"><float.h></A></CODE>,<CODE><A HREF="iso646.html"><iso646.h></A></CODE>,<CODE><A HREF="limits.html"><limits.h></A></CODE>,<CODE><A HREF="stdarg.html"><stdarg.h></A></CODE>, and<CODE><A HREF="stddef.html"><stddef.h></A></CODE>.Each freestanding implementation defines:</P><UL><LI>how it starts the program</LI><LI>what happens when the program terminates</LI><LI>what library functions (if any) it provides</LI></UL><H2><A NAME="Using Standard C Headers">Using Standard C Headers</A></H2><P>You include the contents of a standard header by naming it in an<A HREF="preproc.html#include directive"><I>include</I> directive</A>,as in:</P><PRE>#include <stdio.h> /* include I/O facilities */</PRE><P>You can include the standard headers in any order, a standardheader more than once, or two or more standard headers that definethe same macro or the same type.Do not include a standard header within a declaration. Do notdefine macros that have the same names as keywords before you includea standard header.</P><P>A standard header never includes another standard header. Astandard header declares or defines only the entities described forit in this document.</P><P>Every function in the library is declared in a standard header.The standard header can also provide a<B><A NAME="masking macro">masking macro</A></B>, with the same name asthe function, that masks the function declaration and achieves thesame effect. The macro typically expands to an expression that executesfaster than a call to the function of the same name. The macro can,however, cause confusion when you are tracing or debugging the program.So you can use a standard header in two ways to declare or definea library function. To take advantage of any macro version, includethe standard header so that each apparent call to the function canbe replaced by a macro expansion.</P><P>For example:</P><PRE>#include <ctype.h>char *skip_space(char *p) { while (isspace(*p)) <B>can be a macro</B> ++p; return (p); }</PRE><P>To ensure that the program calls the actual library function,include the standard header and remove any macro definition with an<A HREF="preproc.html#undef directive"><I>undef</I> directive</A>.</P><P>For example:</P><PRE>#include <ctype.h>#undef isspace <B>remove any macro definition</B>int f(char *p) { while (isspace(*p)) <B>must be a function</B> ++p;</PRE><P>You can use many functions in the library without includinga standard header (although this practice is no longer permitted in<A HREF="#C99">C99</A> and is generally not recommended). Ifyou do not need defined macros or types to declare and call the function,you can simply declare the function as it appears in this chapter.Again, you have two choices. You can declare the function explicitly.</P><P>For example:</P><PRE> double sin(double x); <B>declared in <math.h></B> y = rho * sin(theta);</PRE><P>Or you can declare the function implicitly if it is a functionreturning <I>int</I> with a fixed number of arguments, as in:</P><PRE> n = atoi(str); <B>declared in <stdlib.h></B></PRE><P>If the function has a<A HREF="stdarg.html#varying number of arguments">varying numberof arguments</A>, such as<A HREF="stdio.html#printf"><CODE>printf</CODE></A>,you must declare it explicitly: Either include the standard headerthat declares it or write an explicit declaration.</P><P>Note also that you cannot define a macro or type definitionwithout including its standard headerbecause each of these typically varies among implementations.</P><H2><A NAME="C Library Conventions">C Library Conventions</A></H2><P>A library macro that<A HREF="#masking macro">masks</A>a function declaration expands toan expression that evaluates each of its arguments once (and onlyonce). Arguments that have<A HREF="function.html#side-effects context">side effects</A>evaluate the same way whether the expression executesthe macro expansion or calls the function.Macros for the functions<A HREF="stdio.html#getc"><CODE>getc</CODE></A> and<A HREF="stdio.html#putc"><CODE>putc</CODE></A>are explicit exceptions to this rule. Their <CODE>stream</CODE>arguments can be evaluated more than once. Avoid argument expressionsthat have side effects with these macros.</P><P>A library function that alters a value stored in memory assumesthat the function accesses no other objects that overlap theobject whose stored value it alters. You cannot depend on consistentbehavior from a library function that accesses and alters the samestorage via different arguments. The function<A HREF="string.html#memmove"><CODE>memmove</CODE></A>is an explicit exception to this rule. Its argumentscan point at objects that overlap.</P><P>An implementation has a set of<B><A NAME="reserved names">reserved names</A></B> that itcan use for its own purposes. All the library names described in thisdocument are, of course, reserved for the library. Don't definemacros with the same names. Don't try to supply your own definitionof a library function, unless this document explicitly says you can(only in C++). An unauthorized replacement may be successful on someimplementations and not on others. Names that begin withtwo underscores (or contain two successive underscores, in C++),such as <CODE>__STDIO</CODE>, and names thatbegin with an underscore followed by an upper case letter, such as<CODE>_Entry</CODE>, can be used as macro names, whether or nota translation unit explicitly includes any standard headers.Names that begin with an underscore can be defined with externallinkage. Avoid writing such names in a programthat you wish to keep maximally portable.</P><P>Some library functions operate on<B><A NAME="C string">C strings</A></B>, or pointers to<A HREF="charset.html#null-terminated string">null-terminated strings</A>.You designate a C stringthat can be altered by an argument expression that has type<I>pointer to char</I> (or type <I>array of char,</I> which converts to<I>pointer to char</I> in an argument expression).You designate a C string that cannot be altered by an argumentexpression that has type <I>pointer to const char</I> (or type<I>const array of char</I>).In any case, the value of the expression isthe address of the first byte in an array object.The first successive element of the array that has a<A HREF="charset.html#null character">null character</A>stored in it marks the end of the C string.</P><UL><LI>A<B><A NAME="filename">filename</A></B> is a stringwhose contents meet the requirementsof the target environment for naming files.</LI><LI>A<B><A NAME="multibyte string">multibyte string</A></B>is composed of zero or more<A HREF="charset.html#Multibyte Characters">multibyte characters</A>,followed by a<A HREF="charset.html#null character">null character</A>.</LI><LI>A<B><A NAME="wide-character string">wide-character string</A></B>is composed of zero or more<A HREF="charset.html#Wide-Character Encoding">wide characters</A>(stored in an array of<A HREF="stddef.html#wchar_t"><CODE>wchar_t</CODE></A>), followed by a<A HREF="charset.html#null wide character">null wide character</A>.</LI></UL><P>If an argument to a library function has a pointer type, thenthe value of the argument expression must be a valid address for anobject of its type. This is true even if the library function hasno need to access an object by using the pointer argument. An explicitexception is when the description of the library function spells outwhat happens when you use a null pointer.</P><P>Some examples are:</P><PRE> strcpy(s1, 0) <B>is INVALID</B> memcpy(s1, 0, 0) <B>is UNSAFE</B> realloc(0, 50) <B>is the same as malloc(50)</B></PRE><H2><A NAME="C Program Startup and Termination">Program Startup and Termination</A></H2><P>The target environment controls the execution of the program(in contrast to the translator part of the implementation, which preparesthe parts of the program for execution). The target environment passescontrol to the program at<B><A NAME="program startup">program startup</A></B>by calling the function<B><A NAME="main"><CODE>main</CODE></A></B>that you define as part of the program.<B><A NAME="program arguments">Program arguments</A></B> are<A HREF="#C string">C strings</A>that the target environment provides, such as text from the<B><A NAME="command line">command line</A></B>that you type to invoke the program. If the programdoes not need to access program arguments, you can define <CODE>main</CODE>as:</P><PRE>extern int main(void) { <I><body of main></I> }</PRE><P>If the program uses program arguments, you define <CODE>main</CODE>as:</P><PRE>extern int main(int argc, char **argv) { <I><body of main></I> }</PRE><P>You can omit either or both of <CODE>extern int</CODE>, sincethese are the default storage class and type for a functiondefinition. For program arguments:</P><UL><LI><CODE>argc</CODE> is a value (always greater than zero) that specifiesthe number of program arguments.</LI><LI><CODE>argv[0]</CODE> designates the first element of an array of<A HREF="#C string">C strings</A>.<CODE>argv[argc]</CODE> designates the last element of the array, whosestored value is a null pointer.</LI></UL><P>For example, if you invoke a program by typing:</P><PRE>echo hello</PRE><P>a target environment can call <CODE>main</CODE> with:</P><UL><LI>The value 2 for <CODE>argc</CODE>.</LI><LI>The address of an array object containing <CODE>"echo"</CODE> storedin <CODE>argv[0]</CODE>.</LI><LI>The address of an array object containing <CODE>"hello"</CODE> storedin <CODE>argv[1]</CODE>.</LI><LI>A null pointer stored in <CODE>argv[2]</CODE>.</LI></UL><P><CODE>argv[0]</CODE> is the name used to invoke the program. The targetenvironment can replace this name with a<A NAME="null string">null string</A> (<CODE>""</CODE>).The program can alter the values stored in <CODE>argc</CODE>,in <CODE>argv</CODE>, and in the array objectswhose addresses are stored in <CODE>argv</CODE>.</P><P>Before the target environment calls <CODE>main</CODE>, it stores theinitial values you specify in all objects that have static duration.It also opens three<B><A NAME="standard streams">standard streams</A></B>,controlled by the text-stream objects designatedby the macros:</P><UL><LI><B><A HREF="stdio.html#stdin"><CODE>stdin</CODE></A></B> --for <B><A NAME="standard input">standard input</A></B></LI><LI><B><A HREF="stdio.html#stdout"><CODE>stdout</CODE></A></B> --for <B><A NAME="standard output">standard output</A></B></LI><LI><B><A HREF="stdio.html#stderr"><CODE>stderr</CODE></A></B> --for <B><A NAME="standard error">standard error</A></B> output</LI></UL><P>If <CODE>main</CODE> returns to its caller, the target environment calls<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>with the value returned from <CODE>main</CODE> as the status argument to<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>. If the<A HREF="function.html#Return Statement"><I>return</I> statement</A> thatthe program executes has no expression, the status argument is undefined.This is the case if the program executes the implied<A HREF="function.html#Return Statement"><I>return</I> statement</A>at the end of the function definition.</P><P>You can also call<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>directly from any expression within the program. In both cases,<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>calls all functions registered with<A HREF="stdlib.html#atexit"><CODE>atexit</CODE></A>in reverse order of registry and then begins<B><A NAME="program termination">program termination</A></B>.At program termination, the target environment closesall open files, removes any temporary files that you created by calling<A HREF="stdio.html#tmpfile"><CODE>tmpfile</CODE></A>,and then returns control to the invoker, using thestatus argument value to determine the termination status to reportfor the program.</P><P>The program can terminate abnormally by calling<A HREF="stdlib.html#abort"><CODE>abort</CODE></A>,for example. Each implementation defines whether it closes files,whether it removes temporary files, and what termination status itreports when a program terminates abnormally.</P><HR><P>See also the<B><A HREF="index.html#Table of Contents">Table of Contents</A></B> and the<B><A HREF="_index.html">Index</A></B>.</P><P><I><A HREF="crit_pb.html">Copyright</A> © 1989-2002by P.J. Plauger and Jim Brodie. All rights reserved.</I></P><!--V4.01:1125--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -