The sole purpose of the header file is to provide the definition of the macro assert. If the assertion is not true (expression == 0), the program will output a prompt message in the standard error stream and cause the program to abort aborting the call to abort ().
Definition: void assert (int expression);
// # define NDEBUG
#include
int main (int argc, char * argv []) {
int a = 12;
int b = 24;
assert (a> b);
printf ("a is larger than b!");
return 0;
}
The above program will find that the program is aborted, printf is not executed, and there is this output: main: Assertion `a> b 'failed. The reason is that because a is actually less than b, the assertion fails, assert outputs an error message, and calls abort ( ) The program execution was aborted.
Character test
It mainly provides two types of important functions: character test function and character size conversion function. The provided functions take int type as a parameter and return a value of int type. The argument type should be converted implicitly or explicitly to type int.
int isalnum (int c); Determine whether it is a letter or number.
int isalpha (int c); Determine whether it is an alphabet.
int iscntrl (int c); Determine whether it is a control character.
int isdigit (int c); Determine whether it is a digit.
int isgraph (int c); Determine whether it is a displayable character.
int islower (int c); Determine whether it is lowercase.
int isupper (int c); Determine whether it is an uppercase letter.
int isprint (int c); Determine whether it is a displayable character.
int ispunct (int c); Determine whether it is a punctuation character.
int isspace (int c); determine whether it is a blank character
int isxdigit (int c); Determine whether the character is hexadecimal.
int tolower (int c); Convert to lowercase letters.
int toupper (int c); Convert to uppercase letters.
(Part) Error codes thrown by library functions
error.h is the header file in the C language C standard function library, which defines a macro that returns error information through an error code:
The errno macro is defined as an lvalue of type int and contains the last error code generated by any function using the errno function.
Some macros that represent error codes, defined as integer values:
EDOM comes from the function parameter out of range, for example sqrt (-1)
ERANGE comes from the result of the function out of range, for example s trtol ("0xfffffffff", NULL, 0)
EILSEQ is derived from an illegal character order, for example wcstombs (str, L "\ xffff", 2)
Floating point arithmetic
The float header file defines the maximum and minimum limits for floating-point values. Floating-point values are defined in the following way: symbol -value E exponent Symbol is positive and negative, value is the value of the number
The following values are defined with #define. These values are detailed implementations, but may not be more detailed than those given here,
In all instances, FLT refers to float, DBL refers to double, and LDBL refers to long double.
FLT_ROUNDS
Define the rounding method for floating-point values, -1 is undefined, 0 is toward 0, 1 is closest, 2 is toward positive infinity, and 3 is negative infinity
FLT_RADIX 2
Define the basic representation of the index (eg base-2 is binary, base-10 is decimal notation, 16 is hexadecimal)
FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG
Define the number of digits in the value
FLT_DIG 6, DBL_DIG 10, LDBL_DIG 10
Can not change the maximum decimal place after rounding
FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP
The smallest negative integer value of the exponent of FLT_RADIX
FLT_MIN_10_EXP -37, DBL_MIN_10_EXP -37, LDBL_MIN_10_EXP -37
The smallest negative integer value of the exponent in decimal notation
FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP
The largest integer value of the index of FLT_RADIX
FLT_MAX_10_EXP +37, DBL_MAX_10_EXP, LDBL_MAX_10_EXP +37 +37
The largest integer value of the exponent in decimal notation
FLT_MAX 1E + 37, DBL_MAX 1E + 37, LDBL_MAX 1E + 37
Floating point maximum
FLT_EPSILON 1E-5, DBL_EPSILON 1E-9, LDBL_EPSILON 1E-9
Minimum signable number
Detect integer data type value range
CHAR_BIT an ASCII character length
SCHAR_MIN character minimum
SCHAR_MAX character maximum
UCHAR_MAX unsigned character maximum
CHAR_MIN
CHAR_MAX
The maximum and minimum value of the char character, if the char character is being represented as a signed integer. Their values are the same as signed integers. Otherwise, the minimum value of the char character is 0, and the maximum value is the maximum value of the unsigned char character.
MB_LEN_MAX The maximum number of bytes occupied by a character
SHRT_MIN shortest integer
SHRT_MAX maximum short shaping
USHRT_MAX maximum unsigned short integer
INT_MIN minimum integer
INT_MAX maximum shaping
UINT_MAX maximum unsigned integer
LONG_MIN minimum long integer
LONG_MAX maximum long integer
ULONG_MAX unsigned long
localization
The set of national, cultural, and language rules is called regional settings, and the functions related to regional settings are defined in the header file. The setlocale function is used to set or return the current regional characteristics, and localeconv is used to return the digital and currency information in the current region (stored in the struct lconv structure instance). The first actual parameter of setlocale specifies the regional behavior category to be changed. The predefined setlocale categories are:
LC_ALL
All localized information
LC_COLLATE
Affect strcoll and strxfrm
LC_CTYPE
Affects character processing functions and multi-line character processing functions
LC_MONETARY
Affects the currency formatting information returned by localeconv
LC_NUMERIC
Affects the decimal symbol in formatted input and output characters
LC_TIME
Affect strftime function
2 functions are provided in the header file
setlocale () Set or restore localized information
localeconv () returns information about the current locale setting
setlocale (constant, location) usage
If this function is successfully executed, it will return the current scene attributes; if it fails, it will return False.
constant parameter (required parameter. Specify the set scene information)
LC_ALL-all subordinate constants
LC_COLLATE – sort order
LC_CTYPE – Character classification and conversion (for example: convert all characters to lowercase or uppercase)
LC_MESSAGES – System information format
LC_MONETARY – currency / currency format
LC_NUMERIC – numeric format
LC_TIME – date and time format
location (required parameter)
Required parameters. Specify the country or region where you need to set scene information. It can consist of a string or an array. If the local area is an array, the setlocale () function will try each array element until it obtains valid language and locale code information from it. This parameter will be very useful if a region is under different names in different operating systems.
struct lconv * localeconv (void); usage
localeconv returns lconv structure pointer lconv structure introduction: save formatted numeric information, save the value including currency and non-currency formatting information, localeconv returns a pointer to the object, the following are the members and information in the structure:
char * decimal_point; decimal point number
char * thousands_sep; Thousands separator of numbers
Each element is the number of digits in the corresponding group, and the higher the index, the more to the left. An element with a value of CHAR_MAX indicates that there are no more groups. An element with a value of 0 means that the preceding element can be used in all groups to the left
char * grouping; number grouping separator
char * int_curr_symbol; The first three characters are the currency symbols specified in ISO 4217, the fourth character is the separator, and the fifth character is '\ 0' * /
char * currency_symbol; local currency symbol
char * mon_decimal_point; Currency decimal point
char * mon_thousands_sep; Thousand group separator
char * mon_grouping; similar to the grouping element
char * positive_sign; sign of positive currency value
char * negative_sign; Sign of negative currency value
char int_frac_digits; fractional part of international currency value
char frac_digits; fractional part of local currency value
char p_cs_precedes; 1 if currency_symbol is placed before the positive currency value, 0 otherwise
char p_sep_by_space; 1 if and only if currency_symbol and positive currency value are separated by a space
char n_cs_precedes; <1 if currency_symbol is placed before the negative currency value, otherwise 0 / dt>
char n_sep_by_space; 1 if and only if currency_symbol and negative currency value are separated by a space
char p_sign_posn; formatting options
0-parentheses around quantity and currency symbols
1-the + sign before the quantity and currency symbol
2-+ sign after quantity and currency symbol
3-The + sign before the currency symbol
4-The + sign after the currency symbol
char n_sign_posn formatting options
0-parentheses around quantity and currency symbols
1-the number before the quantity and currency symbol
2-The number after the quantity and currency symbol
3--before the currency symbol
4-The-sign after the currency symbol
Final tip: You can use the setlocale (LC_ALL, NULL) function to set the scene information as the system default.
Mathematical functions
Is a mathematical function library in C language
Trigonometric function
double sin (double x); sine
double cos (double x); cosine
double tan (double x); tangent
* cot trigonometric function can be realized using tan (PI / 2-x).
Inverse trigonometric function
double asin (double x); the result is between [-PI / 2, PI / 2]
double acos (double x); The result is between [0, PI]
double atan (double x);
Arctangent (Principal value), the result is between [-PI / 2, PI / 2]
double atan2 (double y, double);
Arctangent (full circle value), the result is between [-PI, PI]
Hyperbolic trigonometric function
double sinh (double x); calculate hyperbolic sine
double cosh (double x); calculate hyperbolic cosine
double tanh (double x); calculate hyperbolic tangent
Index and logarithm
double exp (double x); find the power of the natural number e
double sqrt (double x); square root
double log (double x); logarithm to base e
double log10 (double x); logarithm to base 10
double pow (double x, double y);
Calculate x to the base y power
float powf (float x, float y);
Consistent with pow, both input and output are floating point
Rounding
double ceil (double); take the whole
double floor (double); Take off the whole
Standardized floating point
double frexp (double f, int * p);
Standardized floating point number, f = x * 2 ^ p, f is known to find x, p (x is between [0.5, 1])
double ldexp (double x, int p);
Contrary to frexp, given x, p find f
take Rounding and balance
double modf (double, double *);
Pass the integer part of the parameter back through the pointer and return the fractional part
double fmod (double, double);
Returns the remainder of the division of two parameters
"Non-local jump"
In this header file, a special method for calling and returning functions is defined. This way is different from the previous function call and return order,
It allows program flow to immediately return from a deeply nested function.
Two macros are defined in:
int setjmp (jmp_buf env); / * Set transfer point * /
longjmp (jmp_buf jmpb, int retval); / * Jump * /
The function of the macro setjmp is to save the state of the current program in the structure env and set a jump point for calling the macro longjmp. setjmp saves the current information in env for use by longjmp. Among them, env is of jmp_buf structure type.
Demo:
#include
#include
static jmp_buf buf;
void second (void) {
printf ("second \ n");
// print
longjmp (buf, 1);
// jump back to the call of setjmp
-Make setjmp return value 1
}
void first (void) {
second ();
printf ("first \ n");
// It is impossible to execute this line
}
int main () {
if (! setjmp (buf)) {
first ();
// Before entering this line, setjmp returns 0
} else {
// When longjmp jumps back,
setjmp returns 1, so enter this line
printf ("main \ n");
// print
}
return 0;
}
When calling setjmp directly, the return value is 0, which is generally used for initialization (when setting the jump point). Use the env variable to jump when calling the longjmp macro later. The program will automatically jump to the return statement of the setjmp macro. At this time, the return value of setjmp is non-zero, specified by the second parameter of longjmp.
Generally, the macro setjmp and longjmp are used in pairs, so that the program flow can be returned from a deeply nested function.
signal
In the signal.h header file, some functions are provided to process the signals generated during execution.
Macro:
SIG_DFL
SIG_ERR
SIG_IGN
SIGABRT
SIGFPE
SIGILL
SIGINT
SIGSEGV
SIGTERM
function:
signal ();
raise ();
variable:
typedef sig_atomic_t
The sig_atomic_t type is an int type, used to receive the return value of the signal function.
Macros beginning with SIG_ are used to define signal processing functions
SIG_DFL The default signal processing function.
SIG_ERR indicates an error signal, the return value when the signal function fails.
SIG_IGN signal processing function, indicating that the signal is ignored.
The macro at the beginning of SIG is used to represent a signal code under the following conditions:
SIGABRT abnormal termination (abort function generated).
SIGFPE floating point error (error caused by 0 as a divisor, illegal operation).
SIGILL illegal operation (instruction).
SIGINT Signals generated by interactive operations (eg CTRL-C).
SIGSEGV invalid access storage (illegal access to fragments, illegal access to memory).
SIGTERM terminates the request.
signal function
void (* signal (int sig, void (* func) (int))) (int);
In the above function definition, sig represents a signal code (equivalent to the code category), which is the macro beginning with the SIG defined above. When a signal appears (that is, when a secret code is received), the function defined by the parameter func is called. If func is equal to SIG_DFL, it means to call the default processing function. If it is equal to SIG_IGN, it means that this signal is ignored (no processing). If func is a user-defined function, it will first call the default processing function, and then call the user-defined function. The custom function has one parameter, and the parameter type is int, which is used to represent the signal code (signature category). At the same time, the function must end with statements such as return, abort, exit, or longjump. When the custom function finishes running, the program will continue to run from where it was terminated. (Unless the signal is SIGFPE and the result is undefined, you may not be able to continue running)
If the call to the signal function is successful, a pointer is returned, which points to the pre-defined signal processor for the specified signal class.
If the call fails, a SIG_ERR will be returned, and the value of errno will be changed accordingly.
raise function
int raise (int sig);
Send a signal sig. The signal parameter is a macro beginning with SIG.
If the call is successful, it returns 0. Otherwise, a non-zero value is returned.
Variable parameter list
The header file defines some macros to get the function parameters when the function parameters are unknown
Variable: typedef va_list
Macro:
va_start ()
va_arg ()
va_end ()
Variables and definitions
The va_list type is defined by the stdarg macro to access the parameter list of a function, and the end of the parameter list will be omitted with an ellipsis
Declaration: void va_start (va_list ap, last_arg);
Initialize the parameter ap with the va_arg and va_end macros, last_arg is the last fixed parameter passed to the function, the parameter before the ellipsis Note that va_start must be called before using va_arg and va_end
Declaration: type va_arg (va_list ap, type);
Use type to expand to the next parameter in the parameter table
Note that ap must be initialized with va_start, if there is no next parameter, the result will be undefined
Disclaimer: void va_end (va_list ap); Allow a function with a parameter list (using va_start macro) to return. If va_end is not called before returning, the result will be undefined. The parameter variable list may no longer be used (call va_end without calling va_start)
Some constants, types and variables
The header file defines some standard definitions, and many definitions will also appear in other header files
Macro commands: NULL and offsetof ()
Variables and definitions:
ptrdiff_t is the result of subtracting two pointers
size_t is an unsigned integer value obtained by sizeof a keyword
wchar_t is the size of a wide character constant, which is an integer type
NULL is the constant value of the null pointer
offsetof (type, member-designator); This macro returns the offset of a structure member relative to the start address of the structure (in bytes), type is the name of the structure, and member-designator is the name of the structure member .
Input and output
The header file defines functions, types, and macros for input and output. The most important type is FILE used to declare file pointers. The other two commonly used types are size_t and fpos_t. Size_t is an unsigned integer type generated by the operator sizeof; the fpos_t type defines an object that can uniquely describe each position in the file. The most useful macro defined by the header is EOF, and its value represents the end of the file.
Constant:
NULL
_IOFBF indicates full buffering
_IOLBF indicates line buffer
_IONBF means no cache
The size of the buffer used by the BUFSIZ setbuf function
EOF EOF is a negative integer representation END OF FILE
FOPEN_MAX (20) The maximum number of files opened simultaneously
FILENAME_MAX The maximum length of the file name
L_tmpnam integer, maximum length temporary file name
SEEK_CUR get the current document location
SEEK_END when moving the read-write position to the end of the file
SEEK_SET moves the read-write position to the beginning of the file
TMP_MAXtmpnam most times
stderr standard error stream, the default is the screen, which can be output to a file.
stdin standard input stream, default is keyboard
stdout standard output stream, default is screen
All functions (click to view introduction and DEMO):
clearerr (); reset error flag
fclose (); Close a stream.
feof (); Detect end of file
ferror (); Check the stream for errors
fflush (); update buffer
fgetpos (); read and write location of mobile file stream
fopen (); Open file
fread (); read data from file stream
freopen (); Open file
fseek (); read and write position of mobile file stream
fsetpos (); locate the file pointer on the stream
ftell (); Get the reading position of the file stream
fwrite (); write data to file stream
remove (); delete file
rename (); change the file name or location
rewind (); reset the position of the read directory to the beginning
setbuf (); associate buffer with stream
setvbuf (); Associate the buffer with the stream
tmpfile (); Create a temporary binary file in the form of wb +
tmpnam (); produces a unique file name
fprintf (); Format the output data to a file
fscanf (); format string input
printf (); format the output data
scanf (); format input function
sprintf (); format string copy
sscanf (); format string input
vfprintf (); Format the output data to a file
vprintf (); Format the output data
vsprintf (); format string copy
fgetc (); read a character from the file
fgets (); read a string in the file
fputc (); Write a specified character to the file stream
fputs (); Write a specified character string to a file
getc (); read a character from the file
getchar (); read a character from the standard input device
gets (); read a string from the standard input device
putc (); Write a specified character to the file
putchar (); write the specified character to the standard output device
puts (); send a string to the stream stdout
ungetc (); Write the specified character back to the file stream
perror (); print out the error reason information string
Useful functions
The header file contains the most commonly used system functions in C language
Macro:
NULL
EXIT_FAILURE failure status code
EXIT_SUCCESS success status code
RAND_MAX rand's maximum return value
MB_CUR_MAX The maximum number of bytes in a multibyte character
variable:
typedef size_t is unsigned integer type
typedef wchar_t the size of a wide character
struct div_t is the structure type as the return type of the div function
struct ldiv_t is the structure type as the return type of the ldiv function
function:
String function
atof (); Convert string to floating point number
atoi (); Convert string to integer
atol (); Convert string to long integer
strtod (); Convert string to floating point
strtol (); Convert string to long integer
strtoul (); Convert string to unsigned long integer
Memory control function
calloc (); Configure memory space
free (); free the originally configured memory
malloc (); Configure memory space
realloc (); reallocate main memory
Environment function
abort (); Abnormally terminate a process
atexit (); Set the function to call before the program ends normally
exit (); End the process normally
getenv (); Get the contents of environment variables
system (); execute shell commands
Search and sort functions
bsearch (); binary search
qsort (); Use quick sort to arrange arrays
Mathematical functions
abs (); Calculate the absolute value of integer
div (); Divide two integers, return quotient and remainder
labs (); take the long absolute value
ldiv (); Divide two long integers, return quotient and remainder
rand (); random number generator
srand (); set random number seed
Multibyte functions
mblen (); Determine the number of bytes of characters according to the locale settings
mbstowcs (); convert multi-byte character string to wide character string
mbtowc (); Convert multibyte characters to wide characters
wcstombs (); convert wide strings to multibyte strings
wctomb (); convert wide characters to multibyte characters
String function
The header file contains the most commonly used string manipulation functions in C language
Macro:
NULL
variable:
typedef size_t
function:
memchr (); Find a specific character in a memory range
memcmp (); compare memory contents
memcpy (); copy memory contents
memmove (); copy memory contents
memset (); fill a section of memory space with a value
strcat (); connect two strings
strncat (); connect two strings
strchr (); Find the first specified character in the string
strcmp (); compare strings
strncmp (); Compare the first N characters of 2 strings
strcoll (); Use the character arrangement of the current area to compare strings
strcpy (); copy string
strncpy (); copy string
strcspn (); returns the number of characters without consecutive characters
strerror (); returns a description string for the cause of the error
strlen (); Calculate the length of the string
strpbrk (); Find the first specified character in the string
strrchr (); Find the last specified character in the string
strspn (); returns the number of characters in a string without the specified characters
strstr (); Find the specified string in a string
strtok (); Split string
strxfrm (); Convert string
Time and date functions
It is a header file for obtaining time and date, operating and formatting time and date data in the C standard function library.
Macro:
NULL null is the value of a null pointer constant
CLOCKS_PER_SEC clocks per second
variable:
typedef size_t typedef
typedef clock_t type definition
struct tm structure
struct tm {
int tm_sec; / * seconds-the value range is [0,59] * /
int tm_min; / * points-the value range is [0,59] * /
int tm_hour; / * hour-the value range is [0,23] * /
int tm_mday; / * day of the month-the value range is [1,31] * /
int tm_mon; / * month (starting from January, 0 represents January)-the value range is [0,11] * /
int tm_year; / * year, its value is equal to the actual year minus 1900 * /
int tm_wday; / * weekday-the value range is [0,6], where 0 represents Sunday, 1 represents Monday, and so on * /
int tm_yday; / * The number of days since January 1 of each year-the value range is [0,365], where 0 represents January 1, 1 represents January 2, and so on * /
int tm_isdst; / * Daylight saving time identifier, when daylight saving time is implemented, tm_isdst is positive When the daylight saving time is not implemented, tm_isdst is 0; when the situation is not known, tm_isdst () is negative. * /
};
function:
asctime (); Represent time and date in string format
clock (); Determine the processor time
ctime (); convert date and time to string
difftime (); calculate the time difference between two moments
gmtime (); Convert date and time to (GMT) time
localtime (); Get the current local time and date
mktime (); Convert time structure data into elapsed seconds
strftime (); format time
time (); Get the current time
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.