为了定义我要使用的标准库函数,我需要使用哪些头文件?
你需要使用ANSI/ISO标准规定的你应该使用的那些头文件,见表12.2。
有趣的是,这些文件并不一定定义你要使用的函数。例如,如果你要使用宏EDOM,你的编译程序保证你能通过包含(errno.h)得到这个宏,而(errno.h)可能定义了宏EDOM,也可能只包含定义这个宏的头文件。更糟的是,编译程序的下一个版本可能会在另一个地方定义宏EDOM。
因此,你不用去寻找真正定义一个函数的头文件并使用这个文件,而应该使用那个被假定为定义了该函数的头文件,这样做是肯定可行的。
有几个名字在多个头文件中被定义:NULL,size_t和wchar_t。如果你需要其中一个名字的定义,可以使用任意一个定义了该名字的头文件((stddef.h>是一个较好的选择,它不仅小,而且包含了常用的宏定义和类型定义)。
表12.2标准库函数的头文件
----------------------------------------------------------------------
函数 头文件
----------------------------------------------------------------------
abort stdlib. h
abs stdlib. h
acos math. h
asctime time. h
asin math. h
assert assert.h
atan math. h
atan2 math. h
atexit stdlib. h
atof stdlib. h
atoi stdlib. h
atol stdlib. h
bsearch stdlib. h
BUFSIZ stdio. h
calloc stdlib. h
ceil math. h
clearerr stdio. h
clock time. h
CLOCKS-PER-SEC time. h
clock_t time. h
cos math. h
cosh math. h
ctime time. h
difftime time. h
div stdlib. h
div_t stdlib. h
EDOM errno. h
EOF stdio. h
ERANGE errno. h
errno errno. h
exit stdlib. h
EXIT_FAILURE stdlib. h
EXIT_SUCCESS stdlib. h
exp math. h
fabs math. h
fclose stdio. h
feof stdio.h
ferror stdio.h
fflush stdio. h
fgetc stdio.h
fgetpos stdio. h
fgets stdio.h
FILE stdio. h
FILENAME-MAX stdio. h
floor math. h
fmod math. h
fopen stdio. h
FOPEN_MAX stdio. h
fpos_t stdio. h
fpnntf stdio. h
fputc stdio.h
fputs stdio. h
head stdio. h
free stdlib. h
freopen stdio. h
frexp math. h
fscanf stdio. h
fseek stdio. h
fsetpos stdio. h
ftell stdio. h
fwrite stdio. h
getc stdio.h
getchar stdio. h
getenv stdlib. h
gets stdio.h
gmtime time. h
HUGE-VAL math.h
_IOFBF stdio. h
_IOLBF stdio. h
_IONBF stdio. h
isalnum ctype. h
isalpha ctype. h
iscntrl ctype. h
isdigit ctype. h
isgraph ctype. h
islower ctype. h
isprint ctype. h
ispunct ctype. h
isspace ctype. h
isupper ctype. h
isxdigit ctype. h
jmp_buf setjmp. h
labs stdlib. h
LC_ALL locale. h
LC_COLLATE locale. h
LC_CTYPE locale. h
LC_MONETARY locale. h
LC_NUMERIC locale. h
LC_TIME locale. h
struct lconv locale. h
ldexp math. h
ldiv stdlib. h
ldiv_t stdlib. h
localeconv locale. h
localtime time. h
log math. h
log10 math. h
longjmp setjmp. h
L_tmpnam stdio. h
malloc stdlib. h
mblen stdlib. h
mbstowcs stdlib. h
mbtowc stdlib. h
MB_CUR_MAX stdlib. h
memchr string. h
memcmp string. h
memcpy string. h
memmove string. h
memset string. h
mktime time. h
modf math. h
NDEBUG assert. h
NULL locale. h.stddef. h.stdio. h.stdlib. h.string. h.time. h
offsetof stddef. h
perror stdio.h
pow math. h
printf stdio.h
ptrdiff_t stddef. h
putc stdio. h
putchar stdio. h
puts stdio. h
qsort stdlib. h
raise signal. h
rand stdlib. h
RAND_MAX stdlib. h
realloc stdlib. h
remove stdio. h
rename stdio. h
rewind stdio. h
scanf stdio.h
SEEK_CUR stdio. h
SEEK_END stdio. h
SEEK_SET stdio. h
setbuf stdio. h
setjmp setjmp. h
setlocale locale. h
setvbuf stdio. h
SIGABRT signal. h
SIGFPE signal. h
SIGILL signal. h
SIGINT signal. h
signal signal. h
SIGSEGV signal. h
SIGTERM signal. h
sig_atomic_t signal. h
SIG_DFL signal. h
SIG_ERR signal. h
SIG_IGN signal. h
sin math. h
sinh math. h
size_t stddef. h.stdlib. h.string. h
sprintf stdio. h
sqrt math. h
srand stdlib. h
sscanf stdio. h
stderr stdio.h
stdin stdio. h
stdout stdio. h
strcat string. h
strchr string. h
strcmp string. h
strcoll string. h
strcpy string. h
strcspn string. h
strerror string.h
strftime time. h
strlen string. h
strncat string. h
strncmp string. h
strncpy string. h
strpbrk string. h
strrchr string. h
strspn string. h
strstr string. h
strtod stdlib. h
strtok string. h
strtol stdlib. h
strtoul stdlib. h
strxfrm string. h
system stblib. h
tan math. h
tanh math. h
time time. h
time_t time. h
struct tm time. h
tmpfile stdio. h
tmpnam stdio. h
TMP_MAX stdio. h
tolower ctype. h
toupper ctype. h
ungetc stdio. h
va_arg stdarg. h
va_end stdarg. h
valist stdarg. h
va_ start stdarg. h
vfprintf stdio. h
vprintf stdio. h
vsprintf stdio. h
wchar_t stddef. h. stdlib. h
wcstombs stdlib. h
wctomb stdlib. h
-------------------------------------------------------------------------
种假设很可能会使你犯一个危险的错误。另一种危险可能来自对a值的使用,因为当循环结束后,a的值并不一定就是100。
c语言能帮助你解决这样的问题,你可以按如下形式编写这个for循环:
for(a=O;a<100&&Func1(a)!=2;++a)
上述循环清楚地告诉程序员:“从0循环到99,但一旦Func1()等于2就停止循环”。因为整个退出条件非常清楚,所以程序员此后就很难犯前面提到的那些错误了。
函数名和变量名应具有描述性
使用具有描述性的函数和变量名能更清楚地表达代码的意思——并且在某种程度上这本身就是一种注释。以下几个例子就是最好的说明:
y=p+i-c;
和
YearlySum=Principal+Interest-Charges:
哪一个更清楚呢?
p=*(l+o);
和
page=&List[offset];
哪一个更清楚呢?
有趣的是,这些文件并不一定定义你要使用的函数。例如,如果你要使用宏EDOM,你的编译程序保证你能通过包含(errno.h)得到这个宏,而(errno.h)可能定义了宏EDOM,也可能只包含定义这个宏的头文件。更糟的是,编译程序的下一个版本可能会在另一个地方定义宏EDOM。
因此,你不用去寻找真正定义一个函数的头文件并使用这个文件,而应该使用那个被假定为定义了该函数的头文件,这样做是肯定可行的。
有几个名字在多个头文件中被定义:NULL,size_t和wchar_t。如果你需要其中一个名字的定义,可以使用任意一个定义了该名字的头文件((stddef.h>是一个较好的选择,它不仅小,而且包含了常用的宏定义和类型定义)。
表12.2标准库函数的头文件
----------------------------------------------------------------------
函数 头文件
----------------------------------------------------------------------
abort stdlib. h
abs stdlib. h
acos math. h
asctime time. h
asin math. h
assert assert.h
atan math. h
atan2 math. h
atexit stdlib. h
atof stdlib. h
atoi stdlib. h
atol stdlib. h
bsearch stdlib. h
BUFSIZ stdio. h
calloc stdlib. h
ceil math. h
clearerr stdio. h
clock time. h
CLOCKS-PER-SEC time. h
clock_t time. h
cos math. h
cosh math. h
ctime time. h
difftime time. h
div stdlib. h
div_t stdlib. h
EDOM errno. h
EOF stdio. h
ERANGE errno. h
errno errno. h
exit stdlib. h
EXIT_FAILURE stdlib. h
EXIT_SUCCESS stdlib. h
exp math. h
fabs math. h
fclose stdio. h
feof stdio.h
ferror stdio.h
fflush stdio. h
fgetc stdio.h
fgetpos stdio. h
fgets stdio.h
FILE stdio. h
FILENAME-MAX stdio. h
floor math. h
fmod math. h
fopen stdio. h
FOPEN_MAX stdio. h
fpos_t stdio. h
fpnntf stdio. h
fputc stdio.h
fputs stdio. h
head stdio. h
free stdlib. h
freopen stdio. h
frexp math. h
fscanf stdio. h
fseek stdio. h
fsetpos stdio. h
ftell stdio. h
fwrite stdio. h
getc stdio.h
getchar stdio. h
getenv stdlib. h
gets stdio.h
gmtime time. h
HUGE-VAL math.h
_IOFBF stdio. h
_IOLBF stdio. h
_IONBF stdio. h
isalnum ctype. h
isalpha ctype. h
iscntrl ctype. h
isdigit ctype. h
isgraph ctype. h
islower ctype. h
isprint ctype. h
ispunct ctype. h
isspace ctype. h
isupper ctype. h
isxdigit ctype. h
jmp_buf setjmp. h
labs stdlib. h
LC_ALL locale. h
LC_COLLATE locale. h
LC_CTYPE locale. h
LC_MONETARY locale. h
LC_NUMERIC locale. h
LC_TIME locale. h
struct lconv locale. h
ldexp math. h
ldiv stdlib. h
ldiv_t stdlib. h
localeconv locale. h
localtime time. h
log math. h
log10 math. h
longjmp setjmp. h
L_tmpnam stdio. h
malloc stdlib. h
mblen stdlib. h
mbstowcs stdlib. h
mbtowc stdlib. h
MB_CUR_MAX stdlib. h
memchr string. h
memcmp string. h
memcpy string. h
memmove string. h
memset string. h
mktime time. h
modf math. h
NDEBUG assert. h
NULL locale. h.stddef. h.stdio. h.stdlib. h.string. h.time. h
offsetof stddef. h
perror stdio.h
pow math. h
printf stdio.h
ptrdiff_t stddef. h
putc stdio. h
putchar stdio. h
puts stdio. h
qsort stdlib. h
raise signal. h
rand stdlib. h
RAND_MAX stdlib. h
realloc stdlib. h
remove stdio. h
rename stdio. h
rewind stdio. h
scanf stdio.h
SEEK_CUR stdio. h
SEEK_END stdio. h
SEEK_SET stdio. h
setbuf stdio. h
setjmp setjmp. h
setlocale locale. h
setvbuf stdio. h
SIGABRT signal. h
SIGFPE signal. h
SIGILL signal. h
SIGINT signal. h
signal signal. h
SIGSEGV signal. h
SIGTERM signal. h
sig_atomic_t signal. h
SIG_DFL signal. h
SIG_ERR signal. h
SIG_IGN signal. h
sin math. h
sinh math. h
size_t stddef. h.stdlib. h.string. h
sprintf stdio. h
sqrt math. h
srand stdlib. h
sscanf stdio. h
stderr stdio.h
stdin stdio. h
stdout stdio. h
strcat string. h
strchr string. h
strcmp string. h
strcoll string. h
strcpy string. h
strcspn string. h
strerror string.h
strftime time. h
strlen string. h
strncat string. h
strncmp string. h
strncpy string. h
strpbrk string. h
strrchr string. h
strspn string. h
strstr string. h
strtod stdlib. h
strtok string. h
strtol stdlib. h
strtoul stdlib. h
strxfrm string. h
system stblib. h
tan math. h
tanh math. h
time time. h
time_t time. h
struct tm time. h
tmpfile stdio. h
tmpnam stdio. h
TMP_MAX stdio. h
tolower ctype. h
toupper ctype. h
ungetc stdio. h
va_arg stdarg. h
va_end stdarg. h
valist stdarg. h
va_ start stdarg. h
vfprintf stdio. h
vprintf stdio. h
vsprintf stdio. h
wchar_t stddef. h. stdlib. h
wcstombs stdlib. h
wctomb stdlib. h
-------------------------------------------------------------------------
种假设很可能会使你犯一个危险的错误。另一种危险可能来自对a值的使用,因为当循环结束后,a的值并不一定就是100。
c语言能帮助你解决这样的问题,你可以按如下形式编写这个for循环:
for(a=O;a<100&&Func1(a)!=2;++a)
上述循环清楚地告诉程序员:“从0循环到99,但一旦Func1()等于2就停止循环”。因为整个退出条件非常清楚,所以程序员此后就很难犯前面提到的那些错误了。
函数名和变量名应具有描述性
使用具有描述性的函数和变量名能更清楚地表达代码的意思——并且在某种程度上这本身就是一种注释。以下几个例子就是最好的说明:
y=p+i-c;
和
YearlySum=Principal+Interest-Charges:
哪一个更清楚呢?
p=*(l+o);
和
page=&List[offset];
哪一个更清楚呢?