NOTICE
-
不能在头文件中定义全局变量,否则该变量会存在于任何
#include <ThisHeader_H>
的地方。 -
要学会防御式编程,即要保证头文件的幂等性
#ifndef _STDIO_H #define _STDIO_H ... /* Body of <stdio.h> */ #endif
-
一个程序应该可以包含两个定义了相同名字的头文件而不会造成错误
-
例如
size_t
,使用另一个宏保护来防止这种类型的多次定义#ifndef _SIZE_T #define _SIZE_T typedef unsigned int size_t; #endif
-
而例如
NULL
,则不需要考虑这种问题,#define NULL (void*) 0
,标准C允许宏的良性重定义,具有相同宏名的两个定义必须具有相同的记号序列。
-
1. 初级
1.1 <stdio.h>
It defines three variable types, several macros, and various functions for performing input and output.
1.1.1 Library Variables
Variable | Description |
---|---|
size_t |
This is the unsigned int type and is the result of the sizeof keyword. |
FILE |
This is an object type suitable for storing information for a file stream. |
fpos_t |
This is an object type suitable for storing any position in a file. |
1.1.2 Library Macros
Macro | Description |
---|---|
NULL |
It is the value of a null pointer constant. |
_IOFBF , _IOLBF and _IONBF |
These which expand to integral constant expressions with distinct values and suitable for the use as third argument to the setvbuf function. |
BUFSIZ |
It is an int , which represents the size of the buffer used by the setbuf functions. |
EOF |
This macro is a negative integer, which indicates that the end-of-file has been reached. |
FOPEN_MAX |
It is an integer, which represents the maximum number of files that the system can guarantee to be opened simultaneously. |
FILENAME_MAX |
It is an integer, which represents the longest length of a char array suitable for holding the longest possible filename. If the implementation imposes no limit, then this value should be the recommended maximum value. |
L_tmpnam |
It is an integer, which represents the longest length of a char array suitable for holding the longest possible temporary filename created by the tmpnam function. |
SEEK_CUR , SEEK_END , and SEEK_SET |
These are used in the seek function to locate different positions in a file. |
TMP_MAX |
It is the maximum number of unique filenames that the function tmpnam can generate. |
stderr , stdin and stdout |
These are pointers to FILE types which correspond to the standard error, standard input, and standard output systems. |
1.1.3 Library Functions
Function | Description |
---|---|
int fclose(FILE *stream) |
Closes the stream . All buffers are flushed. |
void clearerr(FILE *stream) |
Clears the end-of-file and error indicators for the given stream . |
int feof(FILE *stream) |
Tests the end-of-file indicator for the given stream . |
int ferror(FILE *stream) |
Tests the error indicator for the given stream . |
int fflush(FILE *stream) |
Flushes the output buffer of a stream . |
int fgetpos(FILE *stream, fpos_t *pos) |
Gets the current file position of the stream and writes it to pos . |
FILE *fopen(const char *filename, const char *mode) |
Opens the filename pointed to by filename using the given mode . |
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) |
Reads data from the given stream into the array pointed to by ptr . |
FILE *freopen(const char *filename, const char *mode, FILE *stream) |
Associates a new filename with the given open stream and same time closing the old file in stream. |
int fseek(FILE *stream, long int offset, int whence) |
Sets the file position of the stream to the given offset . The argument offset signifies the number of bytes to seek from the given whence position. |
int fsetpos(FILE *stream, const fpos_t *pos) |
Sets the file position of the given stream to the given position . The argument pos is a position given by the function fgetpos . |
long int ftell(FILE *stream) |
Returns the current file position of the given stream . |
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
Writes data from the array pointed to by ptr to the given stream . |
int remove(const char *filename) |
Deletes the given filename so that it is no longer accessible. |
int rename(const char *old_filename, const char *new_filename) |
Causes the filename referred to, by old_filename to be changed to new_filename . |
void rewind(FILE *stream) |
Sets the file position to the beginning of the file of the given stream. |
void setbuf(FILE *stream, char *buffer) |
Defines how a stream should be buffered. |
int setvbuf(FILE *stream, char *buffer, int mode, size_t size) |
Another function to define how a stream should be buffered. |
FILE *tmpfile(void) |
-
FILE* fopen(const char* fname, const char* mode);
-
int fclose(FILE* file);
-
int fgetc(FILE* in);
-
char* fgets(char* dest, int n, FILE* in);
-
int fputc(int ch, FILE* out);
-
int ungetc(int ch, FILE* in);
-
int printf(const char* format_string...);
-
int scanf(const char* format, ...);
1.2 <ctype.h>
1.2.1 环境变量
environ
保存了当前的环境变量,
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
char *getenv(const char *name);
1.3 <stdlib.h>
1.4 <string.h>
2. 中级
2.1assert.h
#include <assert.h>
void assert(int expression);
#define assert(ignore) ((void) 0)
-
<assert.h>
提供宏assert
的定义 -
用于断言
-
配合宏
NDEBUG
使用 -
-
如果程序中某个包含
<assert.h>
的地方没有定义NDEBUG
,则该头文件就会将宏assert
定义为活动形式,它就可以展开为一个表达式,测试断言,并在断言为假的时候输出一条错误 信息,然后程序终止。 -
反之,如果定义了
NDEBUG
,头文件就会将这个宏定义为不执行任何操作的静止形式。 -
如果你认为不需要断言,则只需要如下添加。但是这种方式,当重新需要断言的时候,必须移除上述
define
,还需要重新编译头文件#define NDEBUG /* disable assertion */ #include <assert.h>
-
另一种方式,是由编译器提供支持,在那里定义NDEBUG说明断言无效。
-
-
宏的参数表面上是一个整型表达式,如果表达式的值为0,宏就会写出一条信息并终止程序的运行。
-
宏不能直接调用库的任何输出函数,也不能引用宏。
2.2 <limits.h>
2.3 <stddef.h>
2.4 <time.h>
3. 高级
3.1 <float.h>
3.2 <math.h>
3.3 <error.h>
3.4 <locale.h>
3.5 <setjmp.h>
3.6 <signal.h>
3.7 <stdarg.h>
库 | 描述 |
---|---|
assert.h |
a macro called assert which can be used to verify assumptions made by the program |
stdio.h |
file input and output |
ctype.h |
character tests |
string.h |
string operations |
math.h |
mathematical functions such as sin() and cos() |
stdlib.h |
utility functions such as malloc() and rand() |
assert.h |
the assert() debugging macro |
stdarg.h |
support for functions with variable numbers of arguments |
setjmp.h |
support for non-local flow control jumps |
signals.h |
support for exceptional condition signals |
time.h |
date and time |
limits.h , float.h |
constants which define type range values such as INT_MAX |
ctype.h
isalpha(ch)
islower(ch)
isspace(ch)
isdigit(ch)
toupper(ch)
,tolower(ch)
string.h
size_t strlen(const char* string);
char* strcpy(char* dest, const char* source);
size_t strlcpy(char* dest, const char* source, size_t dest_size);
char* strcat(char* dest, const char* source);
int strcmp(const char* a, const char* b);
char* strchr(const char* searchIn, char ch);
char* strstr(const char* searchIn, const char* searchFor);
void* memcpy(void* dest, const void* source, size_t n);
void* memmove(void* dest, const void* source, size_t n);
stdlib.h
int rand();
void srand(unsigned int seed);
void* malloc(size_t size);
void free(void* block);
void* realloc(void* block, size_t size);
void exit(int status);
void* bsearch(const void* key, const void* base, size_t len, size_t elem_size, <compare_function>);
void qsort(void* base, size_t len, size_t elem_size, <compare_function>);