当前位置:网站首页>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】
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 strlen、strcpy、strcat 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 scanf、getc 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
边栏推荐
- Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
- Gopher to rust hot eye grammar ranking
- 批量--04---移动构件
- What is fintech? How fintech can help small businesses succeed
- Browsercontext class of puppeter
- Global and Chinese markets of three-phase induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
- 关于组件传值
- Differences between SQL and NoSQL of mongodb series
- 5-5配置Mysql复制 基于日志点的复制
猜你喜欢

acwing 790. The cubic root of a number (floating-point number in half)

连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?

Cookie 和 Session

超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

Multimix: small amount of supervision from medical images, interpretable multi task learning

Sum of acwing796 submatrix

盒马,最能代表未来的零售

批量--04---移动构件

Acwing 797 differential

RTOS RT thread bare metal system and multi thread system
随机推荐
C regular expression
Page class of puppeter
Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
面试:为什么整数包装类尽量用equals()来比较大小
Servlet API
What is fintech? How fintech can help small businesses succeed
pbootcms的if判断失效直接显示标签怎么回事?
Batch --03---cmdutil
h t fad fdads
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.3 open、creat、close、unlink
Multimix: small amount of supervision from medical images, interpretable multi task learning
<山东大学项目实训>渲染引擎系统(八-完)
Interview: do you understand the packing and unpacking operations?
33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
<山东大学项目实训>渲染引擎系统(六)
h t fad fdads
Project training of Shandong University rendering engine system (II)
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
acwing788. 逆序对的数量
Batch --04--- moving components