Standard header files include:
<asset.h> <ctype.h> <errno.h> <float.h> <limits.h> <locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdlib.h><stdio.h> <string.h> <time.h>
1. Standard Definitions (<stddef.h>)
The file <stddef.h> contains some common definitions of the standard library, and it is automatically included whenever any standard header file is included.
This file defines:
Type size_t (the result type of the sizeof operator, which is some unsigned integer);
Type ptrdiff_t (the result type of subtracting two pointers, which is some signed integer);
Type wchar_t (wide character type, an integer type that can hold all the encoding values of the character set supported by the local environment of the system. It also guarantees that the encoding value of the null character is 0);
Symbol constant NULL (null pointer value);
Macro offsetor (this is a macro with parameters, where the first parameter should be a structure type and the second parameter should be a member name of that structure.
offsetor(s,m)
Calculates the offset of member m in the variable of structure type t).
Note: Some definitions also appear in other header files (like NULL).
2. Error Information (<errno.h>)
<errno.h> defines an int type expression errno, which can be viewed as a variable initialized to 0. When some standard library functions encounter an error during execution, they set it to a non-0 value, but any standard library function sets it to 0.
<errno.h> also defines two macros EDOM and ERANGE, both of which are non-0 integer values. When a mathematical function encounters an argument error, it will set errno to EDOM, and if a range error occurs, it will set errno to ERANGE.
3. Input and Output Functions (<stdio.h>)
File opening and closing:
FILE *fopen(const char *filename, const char *mode); int fclose(FILE * stream);
Character input and output:
int fgetc(FILE *fp); int fputc(int c, FILE *fp);
getc and putc are similar to these two functions but are implemented through macro definitions. Typically, they are defined as follows:
#define getchar() getc(stdin) #define putchar(c) putc(c, stdout) int ungetc(int c, FILE* stream); //Push character c back to stream
Formatted input and output:
int scanf(const char *format, ...); int printf(const char *format, ...); int fscanf(FILE *stream, const char *format, ...); int fprintf(FILE *stream, const char *format, ...); int sscanf(char *s, const char *format, ...); int sprintf(char *s, const char *format, ...);
Line-oriented input and output:
char *fgets(char *buffer, int n, FILE *stream); int fputs(const char *buffer, FILE *stream); char *gets(char *s); int puts(const char *s);
Direct input and output:
size_t fread(void *pointer, size_t size, size_t num, FILE *stream); size_t fwrite(const void *pointer, size_t size, size_t num, FILE *stream);
4. Mathematical Functions (<math.h>)
Trigonometric Functions:
Trigonometric Functions |
sin |
cos |
tan |
Inverse Trigonometric Functions |
asin |
acos |
atan |
Hyperbolic Functions |
sinh |
cosh |
tanh |
Exponential and Logarithmic Functions:
Exponential Function with base e |
exp |
Natural Logarithm Function |
log |
Logarithm Function with base 10 |
log10 |
Other Functions:
Square Root |
sqrt |
Absolute Value |
fabs |
Power, the first parameter as the base, and the second is the exponent |
double pow(double, double) |
Remainder of real numbers, the two parameters are the dividend and divisor |
double fmod(double, double) |
Note: All the functions above that do not have specified type characteristics take one parameter, and both the parameter and return value are of type double.
The following functions return double precision values (including functions ceil and floor). In the table below, except for those parameters that are specifically mentioned, all other parameters of the functions are of type double.
Function Prototype |
Meaning Explanation |
ceil(x) |
Returns the smallest integer not less than x (returns the double value corresponding to this integer) |
floor(x) |
Returns the largest integer not greater than x (returns the double value corresponding to this integer) |
atan2(y, x) |
Returns tan-1(y/x), with a value range of [-pi, pi] |
ldexp(x, int n) |
Returns x*2^n |
frexp(x, int *exp) |
Decomposes x into y*2^n, where y is a decimal located in the interval [1/2,1), returned as the function result, and the integer n is returned via the pointer *exp (an int variable address should be provided). When x is 0, the values of both results are 0 |
modf(x, double *ip) |
Decomposes x into its fractional and integer parts, with the fractional part as the function return value and the integer part returned via pointer *ip. |
5. Character Processing Functions (<ctype.h>)
See the table below:
int isalpha(c) |
c is an alphabetic character |
int isdigit(c) |
c is a numeric character |
int isalnum(c) |
c is an alphanumeric character |
int isspace(c) |
c is a space, tab, or newline character |
int isupper(c) |
c is an uppercase letter |
int islower(c) |
c is a lowercase letter |
int iscntrl(c) |
c is a control character |
int isprint(c) |
c is a printable character, including space |
int isgraph(c) |
c is a printable character, excluding space |
int isxdigit(c) |
c is a hexadecimal digit character |
int ispunct(c) |
c is a punctuation character |
int tolower(int c) |
Returns the corresponding lowercase letter when c is an uppercase letter, otherwise returns c itself |
int toupper(int c) |
Returns the corresponding uppercase letter when c is a lowercase letter, otherwise returns c itself |
Note: These functions return non-0 values when the conditions are met. The last two conversion functions return the original character for non-alphabetic parameters.
6. String Functions (<string.h>)
String Functions
All string functions are listed in the table below, with function descriptions using the following conventions: s, t represent (char *) type parameters, cs, ct represent (const char*) type parameters (they should both represent strings). n represents size_t type parameters (size_t is an unsigned integer type), c is an integer parameter (converted to char):
Function Prototype |
Meaning Explanation |
size_t strlen(cs) |
Returns the length of cs |
char *strcpy(s,ct) |
Copies ct to s. s must specify a sufficiently large character array |
char *strncpy(s,ct,n) |
Copies at most n characters from ct to s. s must specify a sufficiently large character array. If there are not enough characters in ct, fill s with null characters. |
char *strcat(s,ct) |
Copies characters from ct to the existing string in s. s must specify a character array that contains a string and is sufficiently large. |
char *strncat(s,ct,n) |
Copies at most n characters from ct to the existing string in s. s must specify a character array that contains a string and is sufficiently large. |
int strcmp(cs,ct) |
Compares the sizes of strings cs and ct, returning positive, 0, or negative values when cs is greater than, equal to, or less than ct, respectively. |
int strncmp(cs,ct,n) |
Compares the sizes of strings cs and ct, comparing at most n characters. Returns positive, 0, or negative values when cs is greater than, equal to, or less than ct, respectively. |
char *strchr(cs,c) |
Searches for c in cs and returns the first occurrence of c, represented by a pointer to that position. Returns NULL if c is not found in cs |
char *strrchr(cs,c) |
Searches for c in cs and returns the last occurrence of c, or NULL if not found |
size_t strspn(cs,ct) |
Determines the length of a sequence consisting entirely of characters from ct starting from cs |
size_t strcspn(cs,ct) |
Determines the length of a sequence consisting entirely of characters not in ct starting from cs |
char *strpbrk(cs,ct) |
Searches for characters from ct in cs, returning the position of the first character that meets the condition, or NULL if not found |
char *strstr(cs,ct) |
Searches for string ct in cs (sub-string search), returning the position of the first occurrence of ct as a substring of cs, or NULL if ct does not appear in cs |
char *strerror(n) |
Returns a string of error information associated with error number n (pointer to that error information string) |
char *strtok(s,ct) |
Searches for words formed by the characters in ct as delimiters in s |
Memory Operations
<string.h> also contains a set of character array operation functions (memory operation functions), all starting with mem, implemented in a certain efficient manner. In the following prototypes, the types of parameters s and t are (void *), cs and ct are (const void *), n is size_t, and c is int (converted to unsigned char).
Function Prototype |
Meaning Explanation |
void *memcpy(s,ct,n) |
Copies n characters from ct to s, returning s |
void *memmove(s,ct,n) |
Copies n characters from ct to s, returning s, allowing the two segments to overlap |
int memcmp(cs,ct,n) |
Compares n characters starting from cs and ct, returning values defined similarly to strcmp |
void *memchr(cs,c,n) |
Searches for the first occurrence of c in cs within a range of n characters, returning the pointer value of that position, or NULL if not found |
void *memset(s,c,n) |
Sets the first n characters of s to c, returning s |
7. Functional Functions (<stdlib.h>)
Random Number Functions:
Function Prototype |
Meaning Explanation |
int rand(void) |
Generates a random integer from 0 to RAND_MAX |
void srand(unsigned seed) |
Sets the seed value for subsequent random number generation using seed |
Dynamic Memory Allocation Functions:
Function Prototype |
Meaning Explanation |
void *calloc(size_t n, size_t size) |
Allocates a block of memory sufficient to hold n objects of size size, initializing all bytes to 0. Returns the address of that memory block. Returns NULL if unable to fulfill the request |
void *malloc(size_t size) |
Allocates a block of memory sufficient to hold size, returning the address of that memory block, or NULL if unable to fulfill the request |
void *realloc(void *p, size_t size) |
Resizes the memory block pointed to by p to size, returning the address of the new block. If the request can be fulfilled, the content of the new block is the same as the original block; if not, returns NULL, leaving the original block unchanged |
void free(void *p) |
Frees previously allocated dynamic memory blocks |
Several Integer Functions
Several simple integer functions are shown in the table below, where div_t and ldiv_t are two predefined structure types used to store the quotient and remainder obtained from integer division. The components of div_t are int type quot and rem, while those of ldiv_t are long type quot and rem.
Function Prototype |
Meaning Explanation |
int abs(int n) |
Returns the absolute value of an integer |
long labs(long n) |
Returns the absolute value of a long integer |
div_t div(int n, int m) |
Calculates n/m, storing the quotient and remainder in the corresponding members of the result structure |
ldiv_t ldiv(long n, long m) |
Same as above, parameters are long integers |
Numeric Conversion
Function Prototype |
Meaning Explanation |
double atof(const char *s) |
Constructs a double precision value from string s |
int atoi(const char *s) |
Constructs an integer value from string s |
long atol(const char *s) |
Constructs a long integer value from string s |
Execution Control
1) Abnormal termination function abort.
Prototype is:
void abort(void);
2) Normal termination function exit.
Prototype is:
void exit(int status);
Causes the program to terminate immediately in a normal manner. status is the exit value sent to the execution environment, where 0 indicates successful termination, with two available constants: EXIT_SUCCESS, EXIT_FAILURE.
3) Normal termination registration function atexit.
Prototype is:
int atexit(void (*fcn)(void));
This function can register some functions as termination actions. The registered functions should be parameterless and return nothing. When registration is successful, atexit returns 0, otherwise returns a non-zero value.
Interacting with the Execution Environment
1) Function system for sending commands to the execution environment.
Prototype is:
int system(const char *s);
Passes string s to the program’s execution environment to be executed as a system command. If called with NULL as the parameter, the function returns non-0 indicating the presence of a command interpreter in the environment. If s is not NULL, the return value is determined by the implementation.
2) Function getenv for accessing the execution environment.
Prototype is:
char *getenv(const char *s);
Retrieves the environment string associated with the string s from the execution environment. Returns NULL if not found. The specific result of this function is determined by the implementation. In many execution environments, this function can be used to check the values of “environment variables”.
Common Functions bsearch and qsort
1) Binary search function bsearch:
void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *keyval, const void *datum));
The function pointer parameter cmp should be a function similar to the string comparison function strcmp, determining the order of sorting, returning positive, zero, or negative values when the first parameter keyval is greater than, equal to, or less than the second parameter datum, respectively.
2) Quick sort function qsort:
void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *));
The requirements for the comparison function cmp for qsort are the same as for bsearch. Given an array base[0],…,base[n-1], with element size size, qsort can rearrange the elements of this array in ascending order as determined by cmp.