"Many engineers like to encapsulate some existing functions in the standard library. In fact, the functions encapsulated by themselves are not necessarily better than the standard library. Sometimes, the code is more redundant and has bugs. Let's share some common standard libraries of C language.
Standard header files include:
1、 Standard definition ()
The file contains some common definitions of the standard library. No matter which standard header file we include, it will be automatically included.
This document defines:
Type size_ T (the result type of sizeof operator, which is an unsigned integer);
Type ptrdiff_ T (the result type of subtraction between two pointers, which is a signed integer);
Type wchar_ T (wide character type) is an integer type, which is sufficient to store all encoded values of character sets in all local environments supported by the system. Here, the encoding value of the null character is also guaranteed to be 0);
Symbolic Constant null (null pointer value);
Macro offsetot (this is a macro with parameters. The first parameter should be a structure type and the second parameter should be a structure member name.
offsetot(s,m)
Find the offset of member m in the variable of structure type T).
Note: some of these definitions also appear in other header files (such as null).
2、 Error message ()
An expression errno of type int is defined, which can be regarded as a variable with an initial value of 0. When there is an error in the execution of some standard library functions, it is set to a non-0 value, but any standard library function is set to 0.
There are also two macros, Edom and ERANGE, which are non-zero integer values. In case of parameter error during the execution of mathematical function, errno will be set to Edom. In case of value range error, errno will be set to ERANGE.
3、 Input / output function ()
File open and close:
FILE *fopen(const char *filename, const char *mode); int fclose(FILE * stream); Character input / 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. There are usually the following definitions:
#define getchar() getc(stdin)#define putchar(c) putc(c, stdout)int ungetc(int c, FILE* stream); // Return character c to stream
Format I / O:
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 input / 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 I / O:
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 function ()
1. Trigonometric function:
trigonometric function
sin
cos
tan
Inverse trigonometric function
asin
acos
atan
Hyperbolic function
sinh
cosh
tanh
2. Exponential and logarithmic functions:
Exponential function based on e
exp
Natural logarithmic function
log
Base 10 logarithmic function
log10
3. Other functions:
square root
sqrt
absolute value
fabs
Power, the first parameter is the base, and the second is the exponent
double pow(double, double)
The remainder of a real number. The two parameters are divisor and divisor respectively
double fmod(double, double)
Note: all functions without type characteristics above take a parameter, and their parameters and return values are of double type.
The following functions return double precision values (including the functions ceil and floor). In the following table, the parameters of all functions are of double type except those specially specified.
Function prototype
Meaning interpretation
ceil(x)
Find the minimum integer not less than x (return the double value corresponding to this integer)
floor(x)
Find the maximum integer not greater than x (return the double value corresponding to this integer)
atan2(y, x)
Tan-1 (Y / x) is obtained, and the range of its value is [- Pai, Pai]
ldexp(x, int n)
Find x * 2n
frexp(x, int*exp)
Decompose x into y * 2n, which is a decimal in the interval [1 / 2,1), which is returned as the result of the function, and the integer n is returned through the pointer * exp (an int variable address should be provided). When x is 0, the value of both results is 0
modf(x, double
*ip)
Decompose x into decimal part and integer part. The decimal part is used as the return value of the function, and the integer part is returned through the pointer * IP.
5、 Character handler ()
See the following table:
int isalpha(c)
C is an alphabetic character
int isdigit(c)
C is a numeric character
int isalnum(c)
C is an alphabetic or numeric character
int isspace(c)
C is space, tab, line feed
int isupper(c)
C is a capital letter
int islower(c)
C is a lowercase letter
int iscntrl(c)
C is the control character
int isprint(c)
C is a printable character, including spaces
int isgraph(c)
C is a printable character, excluding spaces
int isxdigit(c)
C is a hexadecimal numeric character
int ispunct(c)
C is a punctuation mark
int tolower(int c)
When C is an uppercase letter, the corresponding lowercase letter is returned; otherwise, C itself is returned
int toupper(int c)
When C is a lowercase letter, the corresponding uppercase letter is returned; otherwise, C itself is returned
Note: when the condition holds, these functions return non-zero values. The last two conversion functions return the original character for non alphabetic parameters.
6、 String function ()
1. String function
All string functions are listed in the following table. The function description adopts the following Convention: s and t represent parameters of type (char *), CS and CT represent parameters of type (const char *) (they should all represent strings). N means size_ Parameter of type T (size)_ T is an unsigned integer type), C is an integer parameter (converted to char in the function):
Function prototype
Meaning interpretation
size_ t strlen(cs)
Find the length of CS
char *strcpy(s,ct)
Copy CT to s. Requires s to specify a sufficiently large character array
char *strncpy(s,ct,n)
Copy up to n characters in CT to s. Requires s to specify a large enough character array. If there are not enough n characters in CT, fill in s with empty characters.
char *strcat(s,ct)
Copy the characters in CT to the existing string in S. S should specify a large enough character array to hold the string.
char *strncat(s,ct,n)
Copy up to n characters in CT to the existing string in S. S should specify a large enough character array to hold the string.
int strcmp(cs,ct)
Compare the sizes of the strings CS and CT, and return positive, 0 and negative values respectively when CS is greater than, equal to and less than CT.
int strncmp(cs,ct,n)
Compare the sizes of the strings CS and CT, up to n characters. When CS is greater than, equal to and less than CT, it returns positive, 0 and negative values respectively.
char *strchr(cs,c)
Find C in CS and return the position where C first appears, represented by a pointer to this position. When there is no C in CS, the return value is null
char *strrchr(cs,c)
Search for C in CS and return the last position where C appears. If not, return null
size_ t strspn(cs,ct)
A sequence consisting of all characters in CT is determined from CS, and its length is returned
size_ t strcspn(cs,ct)
Starting from CS, determine a sequence composed of all characters in non CT, and return its length
char *strpbrk(cs,ct)
Search for the characters in CT in CS and return the position where the first qualified character appears. If not, return null
char *strstr(cs,ct)
Search the string CT (query substring) in CS and return the position where CT appears first as the substring of CS. When CT does not appear in CS, it returns null
char *strerror(n)
Returns the error information string related to error number n (pointer to the error information string)
char *strtok(s,ct)
Search in s for words formed by characters in CT as separators
2. Storage area operation
There is also a set of character array operation functions (storage area operation functions), whose names start with MEM and are implemented in an efficient way. In the following prototype, the types of parameters s and T are (void *), CS and CT are (const void *), and N is size_ t. The type of C is int (converted to unsigned char).
Function prototype
Meaning interpretation
void *memcpy(s
Our other product: