当前位置:网站首页>The C programming language (2nd Edition) notes / 7 input and output / 7.8 other functions

The C programming language (2nd Edition) notes / 7 input and output / 7.8 other functions

2022-06-12 16:23:00 M rookie M

Catalog 、 reference


7.8 Other functions

The standard library provides many functions with different functions , This section provides a brief overview of the functions that are particularly useful
For more details and many other functions that are not covered, see the appendix B

7.8.1 String manipulation functions

As mentioned earlier, string functions strlenstrcpystrcat and strcmp It's all in the header file <string.h> In the definition of
In the following functions ,s And t by char * type ,c And n by int type

function describe
strcat(s, t) take t The string pointed to is connected to s To the end of the string
strncat(s, t, n) take t In the string pointed to n Characters to s To the end of the string
strcmp(s, t) according to s The string pointed to is less than (s < t)、 be equal to (s == t) Or greater than (s > t)t Different situations of the string pointed to , Return negative integers respectively 、0 Or a positive integer
strncmp(s, t, n) Same as strcmp identical , But only before n Compare... In characters
strcpy(s, t) take t Copy the string to s Point to
strncpy(s, t, n) take t In the string pointed to n Characters copied to s Point to
strlen(s) return s The length of the string pointed to
strchr(s, c) stay s Find... In the string pointed to c, If you find , Returns a pointer to the position where it first appears , Otherwise return to NULL
strrchr(s, c) stay s Find... In the string pointed to c, If you find , Returns a pointer to the position where it last appeared , Otherwise return to NULL

7.8.2 Character category test and conversion function

The header file <ctype.h> Some functions for character testing and conversion are defined in
In the following functions ,c Is a can be expressed as unsigned char Type or EOF Of int object
The return value of this function is of type int

function describe
isalpha if c It's the letters , Return a non 0 value , Otherwise return to 0
isupper if c It's capital letters , Return a non 0 value , Otherwise return to 0
islower if c It's lowercase , Return a non 0 value , Otherwise return to 0
isdigit if c It's the number. , Return a non 0 value , Otherwise return to 0
isalnum if isalpha(c) or isdigit(c), Return a non 0 value , Otherwise return to 0
isspace if c Is a space 、 Horizontal tabs 、 A newline 、 A carriage return , Page feed or vertical tab , Return a non 0 value
toupper return c In capital form
tolower return c In lowercase

7.8.3 ungetc function

The standard library provides a tool called ungetc Function of , It with the first 4 Functions written in Chapter ungetch More limited than functionality

int ungetc(int c, FILE *fp)

This function converts the character c Write back to the file fp in
If it works , Then return to c, Otherwise return to EOF
Each file can only receive one writeback character
ungetc Function can be used with any input function , such as scanfgetc or getchar

7.8.4 Command execution function

function system(char* s) The execution is contained in the character declaration s The command , Then continue to execute the current program
s The content of is largely related to the operating system used
UNIX Under the operating system environment ,system("date"); The program will be executed date
It prints the date and time of the day on standard output
system Function returns an integer status value , Its value comes from the command executed , And related to the specific system
stay UNIX In the system , The status returned is exit The return value of

7.8.5 Storage management functions

function malloc and calloc Used to dynamically allocate storage blocks , function malloc Statement of :

void *malloc(size_t n)

On successful assignment , Return a pointer , Point to n Uninitialized storage space of byte length , Otherwise return to NULL

function calloc Statement of :

void *calloc(size_t n, size_t size)

When the allocation is successful , It returns a pointer , The free space pointed to is enough to accommodate the free space provided by n An array of objects of specified length , Otherwise return to NULL
The storage space is initialized to 0

According to the requested object type ,malloc or calloc The pointer returned by the function meets the correct alignment requirements
The following example performs type conversion :

int *ip; 
ip = (int *) calloc(n, sizeof(int));

free(p) Function to release p Point to the storage space
among ,p It is by calling malloc or calloc Function gets a pointer to
There is no restriction on the order in which storage space is released
however , If you release a malloc or calloc The storage space pointed to by the pointer obtained by the function , Would be a serious mistake

It is also wrong to use the storage space that has been released
The code shown below is a typical error code snippet , It releases items in the list through a loop :

for (p = head; p != NULL; p = p->next) /* WRONG */ 
    free(p);

The right way to deal with it is , Save all necessary information before releasing the project , As shown below :

for (p = head; p != NULL; p = q) {
     
    q = p->next; 
    free(p); 
}

8.7 Section gives an example similar to malloc Function of the storage allocation program
The storage blocks allocated by the storage allocator can be released in any order

7.8.6 Mathematical functions

The header file <math.h> The statement is made. 20 Multiple mathematical functions
Here are some commonly used mathematical functions , Each function has one or two double Parameters of type , And return a double Type value

function describe
sin(x)x The sine function of , among x In radians
cos(x)x Cosine function of , among x In radians
atan2(y, x)y/x The arctangent function of , among ,x and y In radians
exp(x) Exponential function e^x
log(x)x The natural logarithm of ( With e Bottom ), among ,x > 0
log10(x)x The common logarithm of ( With 10 Bottom ), among ,x > 0
pow(x, y) Calculation x^y Value
sqrt(x)x The square root of (x ≥ 0)
fabs(x)x The absolute value of

7.8.7 Random number generator function

function rand() Build between 0 and RAND_MAX Between the pseudo-random integer sequences
among RAND_MAX Is in header file <stdlib.h> Symbolic constants defined in
The following is a method of generating greater than or equal to 0 But less than 1 Random floating point number method :

#define frand() ((double) rand() / (RAND_MAX + 1.0))

If a function for generating floating-point random numbers has been provided in the function library used , Then it may have better statistical characteristics than the above function

function srand(unsigned) Set up rand The number of seeds of the function
We are 2.7 The following standards are given in section rand and srand Portable implementation of function


Catalog 、 reference

原网站

版权声明
本文为[M rookie M]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121615253951.html