admin 管理员组

文章数量: 1087139


2024年1月17日发(作者:qtpaintevent触发)

C standard library(c标准库)

: diagnosis

defines only a macro assert with a reference, which

is defined as follows:

Void assert (int expression)

The assert macro is used to add diagnostic features to a program,

which can test a condition and possibly terminate the program.

Execute statement:

Assert (expression);

When the expression is 0, a message is displayed at the

terminal:

Assertion failed: 0, file source file name, line line number

Abnormal program termination

Then invoke the abort to terminate the execution of the program.

In , the macro assert is defined as conditional

compilation, and if the macro NDEBUG is defined in the source

file, the assert macro will be ignored even if the header file

is included

: character class testing

Functions that define test characters are defined in header

file . In these functions, the arguments for each

function are integer int, and each parameter is either EOF or

char type. The list of standard functions defined in

is as follows:

Functions defined in

Function definition

Function brief introduction

Int isalnum (int c)

Check whether the characters are letters or numbers

Int isalpha (int c)

Check if the characters are letters

Int isascii (int c)

Check if the character is ASCII

Int iscntrl (int c)

Check that the character is a control character

Int isDigit (int c)

Check that the characters are numeric characters

Int isgraph (int c)

Check that the characters are printable characters

Int islower (int c)

Check if the characters are lowercase letters

Int isprint (int c)

Check that the characters are printable characters

Int ispunct (int c)

Check if the characters are punctuation characters

Int isspace (int c)

Check if the character is a space character

Int isupper (int c)

Check if the characters are uppercase letters

Int isxdigit (int c)

Check if the character is a sixteen digit numeric character

Int toupper (int c)

Converts lowercase letters to uppercase letters

Int tolower (int c)

Converts uppercase letters to lower case letters

These standard functions defined in , and some

commonly used nonstandard character processing functions, will

be described in detail in the Eleventh chapter.

: error handling

Two constants, one variable, are defined in .

1, EDOM

It represents the wrong code for mathematical domain errors.

2, ERANGE

It represents an error code that results out of bounds.

3, errno

This is a variable that is set to indicate the type of error

in the system call.

: integer constant

In header file , some constants that represent

integer sizes are defined. The following are the expressions

and meanings of these constants, as shown in the following

table.

Character constants defined in

character constants

Value

Meaning

CHAR_BIT

Eight

Number of char types

CHAR_MAX

255 or 127

Char type maximum

CHAR_MIN

0 or -127

Char type minimum

INT_MIN

-32767

Int type minimum

INT_MAX

Thirty-two thousand seven hundred and sixty-seven

Int type maximum

LONG_MAX

Two billion one hundred and forty-seven million four hundred

and eighty-three thousand six hundred and forty-seven

Long type maximum

LONG_MIN

-2147483647

Long type minimum

SCHAR_MAX

One hundred and twenty-seven

Signed char type maximum

SCHAR_MIN

-127

Signed char type minimum

SHRT_MAX

Thirty-two thousand seven hundred and sixty-seven

Maximum value of type short

SHRT_MIN

-32767

The minimum value of the short type

UCHAR_MAX

Two hundred and fifty-five

unsigned char类型最大值

uint_max

六万五千五百三十五

unsigned int类型最大值

ulong_max

四十二亿九千四百九十六万七千二百九十五

无符号长类型最大值

ushrt_max

六万五千五百三十五

无符号短类型的最大值

< H >:地域环境现场。

在<现场。”中,定义了7个常量,一个结构,2个函数。

1、常量的定义

lc_all:传递给setlocale的第一个参数,指定要更改该现场的哪个方面。

lc_collate:strcoll和strxfrm的行为。

lc_ctype:字符处理函数。

lc_monetary:localeconv返回的货币信息。

lc_numeric:localeconv返回的小数点和货币信息。

lc_time:strftime的行为。

以上扩展成具有唯一取值的整型常数表达式,可作为setlocale的第一个参数。

空:由实现环境定义的空指针。

2、结构lconv结构

该结构用于存储和表示当前的设置其结构定义如下现场:

结构lconv

{

char * decimal_point;

char * thousands_sep;

字符分组;

char * int_curr_symbol;

char * currency_symbol;

char * mon_decimal_point;

char * mon_thousands_sep;

char * mon_grouping;

char * positive_sign;

char * negative_sign;

焦int_frac_digits;

焦frac_digits;

焦p_cs_precedes;

焦p_sep_by_space;

焦n_cs_precedes;

焦n_sep_by_space;

焦p_sign_posn;

焦n_sign_posn;

};

3、函数

struct iconv * localeconv(void);

函数localeconv将一个iconv类型的对象的数据成员设置成为按照当前地域环境的有关规则进行数量格式化后的相应值结构。

char * setlocale(int类型,char *现场);

函数setlocale用于更改和查询程序的整个当前地域环境或部分设置。地域环境变量由参数范畴(上面定义的6个常量)和现场指定。

< H >:数学函数数学。

在<<数学。”中定义了一些数学函数和宏,用来实现不同种类的数学

运算。下面给出<<数学。”中标准数学函数的函数定义及功能简介,见下表。

< H >中定义的函数数学。

函数定义

函数功能简介

双输出(双x);

指数运算函数,求E的X次幂函数

双对数(双x)

对数函数ln(x)

双log10(x);

对数函数日志

双武器(双x,双Y);

指数函数(X,Y的次方)

双sqrt(x);

计算平方根函数

双细胞(x);

向上舍入函数

双层(双x);

向下舍入函数

双晶圆厂(x);

求浮点数的绝对值

双ldexp(double x,

装载浮点数函数

双frexp(double x,

分解浮点数函数

双修正(x的双IP);

分解双精度数函数

双FMOD(x,y);

求模函数

双罪(双X);

计算X的正弦值函数

双余弦(双x);

);

口);int nint *

计算X的余弦值函数

双色(双X);

计算X的正切值函数

双在(x);

计算X的反正弦函数

双ACOS(x);

计算X的反余弦函数

双以(x);

反正切函数1

双atan2(双y,x);

反正切函数2

双sinh(x);

计算X的双曲正弦值

双cosh(x);

计算X的双曲余弦值

双tanh(x);

计算X的双曲正切值

在标准库中,还有一些与数学计算有关的函数定义在其他头文件中。

< >:非局部跳转setjmp。H

在头文件< setjmp。H >中定义了一种特别的函数调用和函数返回顺序的方式这种方式不同于以往的函数调用和返回顺序,它允许程序流程立即从一个深层嵌套的函数中返回。

< H >中定义了两个宏setjmp:

int setjmp(jmp_buf env);/ * * /设置调转点

longjmp(jmp_buf jmpb,int retval);/ * * /跳转

宏setjmp的功能是将当前程序的状态保存在结构env,为调用宏设置一个跳转点setjmp longjmp。将当前信息保存在env中供longjmp使用。其中env是jmp_buf结构类型的,该结构定义为:

typedef struct {

无符号j_sp;

无符号j_ss;

无符号j_flag;

无符号j_cs;

无符号j_ip;

无符号j_bp;

无符号j_di;

无符号j_es;

无符号j_si;

无符号j_ds;

} jmp_buf [ 1 ];

直接调用setjmp时,返回值为0,这一般用于初始化(设置跳转点时)。以后再调用longjmp宏时用env变量进行跳转。程序会自动跳转到setjmp宏的返回语句处,此时setjmp的返回值为非0,由longjmp的第二个参数指定。

下面通过例子来理解< H >中定义的这两个宏setjmp。

例程9-1非局部跳转演示。

#包括<< setjmp。”

jmp_buf env;/ * * /定义jmp_buf类型变量

主(空)

{

int值;

价值= setjmp(env);/*调用setjmp longjmp,为设置跳转点*/

如果(值)!= 0)

{

printf(“longjmp价值%dn”,值);

退出(价值);/ * * /退出程序

}

printf(“跳„“n);

longjmp(env,1);/*跳转到setjmp语句处*/

返回0;

}

宏为本例程先应用setjmp longjmp设置跳转点,当第一次调用setjmp时返回值为0,并将程序的当前状态(寄存器的相关状态)保存在结构变量env中。当程序执行到longjmp时,系统会根据setjmp保存下来的状态env跳转到setjmp语句处,并根据的第二个参数设置此时setjmp longjmp的返回值。

本例程的运行结果为:

跳„

价值1 longjmp

一般地,宏和setjmp longjmp是成对使用的,这样程序流程可以从一个深层嵌套的函数中返回。

< >:信号H信号。

头文件<信号。

H> provides some of the features that are caused by various

exception conditions during processing, such as interrupts

from outside.

There are only two functions defined in :

Int signal (int, SIG, sigfun, fname);

and

Int raise (int sig);

The function of the signal function is to set the corresponding

action of a signal. The parameter sig is used to specify which

signal is set to the processing function. The signals supported

in standard C are shown below.

Standard C supported signals

Value

Explain

Default execution action

Operating system used

SIGABRT

abort

stay the proceedings

UNIX DOS

SIGPPE

Arithmetic error

stay the proceedings

UNIX DOS

SIGILL

Illegal hardware instruction

stay the proceedings

UNIX DOS

SIGINT

Terminal interrupt

stay the proceedings

UNIX DOS

SIGSEGV

Invalid memory access

stay the proceedings

UNIX DOS

SIGTERM

Abort signal

stay the proceedings

UNIX DOS

The parameter fname is a pointer to the function, and when the

sig signal occurs, the program interrupts automatically and

executes the function pointed to by the fname. When executed,

returns the breakpoint and continues the program. The system

provides two const function pointers that can be passed as

arguments to the function. They are respectively:

SIG_DEF: perform the default function of the system first.

SIG_IGN: ignore this signal.

The function of the raise function is to send a signal to the

program being executed, so that the current process generates

an interrupt, and the steering signal processing function

signal executes. The parameter sig is the signal name, and its

range of value is the same as that of the parameter sig in the

function signal. See table 9-6.

Here are examples of functions signal and raise.

Routines 9-2, signall, and raise functions are demonstrated

#include

#include

Void, Print1 ();

Void, Print2 ();

Int, main ()

{signal (SIGINT, Print1);

Printf ("Please enter Ctr+c for interuptn");

Getchar ();

Signal (SIGSEGV, Print2);

Printf (Please, enter, any, key, for, a, interuptn);

Getchar ();

Raise (SIGSEGV);

}

Void, Print1 ()

{

Printf ("This, is, a, SIGINT, interupt, n");

}

Void, Print2 ()

{

Printf ("This, is, a, SIGSEGV, interupt, n");

}

This routine begins with the user terminal generates a Ctrl+c

input terminal interrupt, and then apply the signal function

calls the interrupt handling function of Print1; then an

invalid memory access through the raise interrupt function

generation and interrupt handling function through Print2

signal function calls.

The running result of this routine is:

Please, enter, Ctr+c, for, interupt

^C

This, is, a, SIGINT, interupt!

Please, enter, any, key, for, a, interupt

A

This, is, a, SIGSEGV, interupt!

: variable parameter table

The macro in the variable parameter table is used

to define functions with variable arguments. In the C language,

some parameters of function library function or user-defined

variable is commonly used, ellipsis"......" (for example,

printf in the library function), defining such functions

requires the use of macros in .

1, va_list

The data type used to hold macros, va_start, va_arg, and va_end

required information.

2, three macros are defined in

Void va_start (va_list, AP, parmN);

Type va_arg (va_list, AP, type);

Void va_end (va_list AP);

The function of va_start is to initialize AP, so va_start should

be used first before all other va_'s macros (except for

variables defined with va_list), followed by va_copy, va_arg,

va_end, and ap. A va_start macro cannot be used again between

a pair of va_start and va_end. Where parmN is the last parameter

before "...". For example, the printf function is defined as:

printf (const, char, *format,...); then, after the va_start in

the printf function is used, the value of parmN is equal to

*format.

The function of va_arg is to return the next parameter with the

type type in the parameter list AP. Each call to va_arg modifies

the value of the AP so that the parameters of the next type type

can be continuously obtained.

Va_end and va_start make up a scope, and va_end marks the end.

After va_end, AP is invalid.

: public definition

In the header file , the common definition in the

standard library is specified. The main contents are as

follows:

1, NULL

Null pointer type constant.

2, offset (type, member-designator)

It is an integer constant expression that extends the iz-t type.

It is the number of offset bytes from the beginning of the

structural type defined by the type to the structural member

member-designator.

3, ptrdiff_t

A signed integer type that represents the difference between

the two pointers.

4, size_t

Represents the result type calculated by the sizeof operator,

which is an unsigned integer type.

5, wchar_t

It is an integer type, with values ranging from all the

characters of the largest extended character set in the

supported geographical environment, and the null character

code to 0.

: I / O

The input and output functions, types, and macros are defined

in the header file . These functions, types, and

macros make up almost 1/3 of the standard library.

Here are the functions and function descriptions declared in

the file , see the following table.

The functions declared in

Function definition

Function brief introduction

FILE *fopen (char, *filename, char, *type)

Open a file

FILE *fropen (char, *filename, char, *type, FILE, *fp)

Open a file and associate the file with the FP specified stream

Int fflush (FILE *stream)

Clear a stream

Int Fclose (FILE *stream)

Close a file

Int remove (char *filename)

Delete a file

Int rename (char, *oldname, char, *newname)

rename file

FILE *tmpfile (void)

Open the temporary file in binary mode

Char *tmpnam (char *sptr)

Creates a unique file name

Int, setvbuf (FILE, *stream, char, *buf, int, type, unsigned,

size)

Buffer and stream correlation

Int printf (char *)

A function that produces formatted output

Int, fprintf (FILE, *stream, char, *format[, argument,...)

Send formatted output to a stream

Int, scanf (char, *format[, argument,...)

Execute formatted input

Int, fscanf (FILE, *stream, char, *format[, )

Performs formatted input from a stream

Int fgetc (FILE *stream)

Reads characters from the stream

Char *fgets (char, *string, int, N, FILE, *stream)

Reads a string from the stream

Int fputc (int, CH, FILE, *stream)

Send one character to one stream

Int fputs (char, *string, FILE, *stream)

Send one character to one stream

Int Getc (FILE *stream)

Take characters from the stream

int getchar (void)

从stdin流中读字符

char * gets (char * string)

从流中取一字符串

int putchar (int ch)

在stdout上输出字符

int putts (char * string)

送一字符串到流中

int ungetc (char c, file * stream)

把一个字符退回到输入流中

int fread (void * ptr, int size, int nitems, file * stream)

从一个流中读数据

int fwrite (void * ptr, int size, int nitems, file * stream)

写内容到流中

int fseek (file * stream, long offset, int fromwhere)

重定位流上的文件指针

long ftell (file * stream)

返回当前文件指针

int rewind (file * stream)

将文件指针重新指向一个流的开头

int fgetpos (file * stream)

取得当前文件的句柄

int fsetpos (file * stream, which fpos _ t * heading)

定位流上的文件指针

void clearerr (file * stream)

复位错误标志

int feof (file * stream)

检测流上的文件结束符

int ferror (file * stream)

检测流上的错误

void perror (char * string)

系统错误信息

在头文件 < stdio.h > 中还定义了一些类型和宏.

< stdlib.h >: 实用函数

在头文件 stdlib.h 中声明了一些实现数值转换 < >, < > 内存分配等类似功能的函数.下面给出头文件 stdlib.h 中声明的函数以及功能简介, 见下表.

< > 中声明的函数 stdlib.h

函数定义

函数功能简介

double fabrics (which char * s)

将字符串s转换为double类型

int atoi (which char * s)

将字符串s转换为int类型

long time (which char * s)

将字符串s转换为long类型

double strtod (which char * s, char * * endp)

将字符串s前缀转换为double型

long strtol (which char * s, char * * endp, int base)

将字符串s前缀转换为long型

unsinged long strtol (which char * s, char * * endp, int base)

将字符串s前缀转换为unsinged long型

int rand (void)

产生一个0 ~ rand _ max之间的伪随机数

void srand (unsigned int seed)

初始化随机数发生器

void * calloc (size _ t nelem, size _ t elsize)

分配主存储器

void * malloc (unsigned size)

内存分配函数

void * realloc (void * ptr, unsigned newsize)

重新分配主存

void free (void * ptr)

释放已分配的块

void abortion (void)

异常终止一个进程

void exit (int status)

终止应用程序

int atexit (atexit _ t func)

注册终止函数

char * getenv (char * envvar)

从环境中取字符串

void * bsearch (which void * key, which void * base, size _ t

* nelem, size _ t width, int (* fcmp) (which void * which *))

二分法搜索函数

qsort void (void * base, nelem width int, int, int (* fcmp) ())

使用快速排序例程进行排序

int abs (int i)

求整数的绝对值

long labs (long n)

取长整型绝对值

div _ t div (int number, int denom)

将两个整数相除, 返回商和余数

ldiv _ t ldiv (long lnumer,

Long ldenom)

Two long integer division, returns the quotient and remainder

About the functions, usages, routines, etc of these standard

practical functions listed above.

: string function

Some string functions are defined in the header file .

They can be divided into two groups. The first set of function

names begins with STR, primarily for string operations; the

second set of function names begin with MEM and manipulate

objects in the form of an array of characters. Here are the

functions and function descriptions declared in the file

, see the following table.

The functions declared in

Function definition

Function brief introduction

Char *strcpy (char, *str1, char, *str2)

String copy function

Char *strncpy (char, *d, char, *s, int, m)

String copy function

Char *strcat (char, *destin, char, *source)

String concatenation function

Char *strncat (char, *d, char, *s, int, n)

String concatenation function

Int StrCmp (char, *str1, char, *str2)

String comparison function

Int strncmp (char, *str1, char, *str2, int, m)

String comparison function

Char *strchr (char, *str, char, c)

Finds the first match of a given character in a string

Char *strrchr (char, *str, char, c)

Finds the last occurrence of the specified character in the

string

Size_t strspn (char, *str1, char, *str2)

The first occurrence of a subset of the specified character set

in a string

Size_t strcspn (char, *str1, char, *str2)

Searches for the first segment of a given character set in a

string

Char *strpbrk (char, *str1, char, *str2)

Finds characters in a given character set in a string

Char *strstr (char, *str1, char, *str2)

Finds the first occurrence of a specified string in a string

Size_t strlen (char *cs)

String length

Char *strerror (int errnum)

Returns the pointer to the error message string

Char *strtok (char, *str1, char, *str2)

Searches for words separated from the delimiter specified in

the second strings

Void *memcpy (void, *d, void, *s, unsigned, n)

Copy n bytes from the source s to the target D

Void *memmove (void, *d, void, *s, unsigned, n)

Move a block of bytes

Void *memcmp (void, *s1, void, *s2, unsigned, n)

Compare the first n bytes of the two strings S2 and S1

Void *memchr (void, *s, char, CH, unsigned, n)

Search for characters in the first n bytes of an array

Void *memset (void, *s, char, CH, unsigned, n)

Replace the previous n character of s with the character Ch

: date and time function

In the header file , some types and functions that deal

with date and time are declared. Clock_t and time_t are two

arithmetic types that represent time values. The structure

struct TM stores the components of a calendar time. The

significance of the members of the structure TM and its normal

range of values are as follows:

Struct TM {

Int tm_sec; / * number of seconds after the beginning of minutes

from the current (0,61).

Int tm_min; a few minutes after the hour from the start / * *

/ (0,59)

Int tm_hour; / * start after hours from midnight (0,23).

Int tm_mday; / * days of the month (1,31).

Int tm_mon; / * from January after the number of months (0,11).

Int tm_year; the number of years since 1900 after the / *

Int tm_wday;

Days after the beginning of this week from / on Sunday (0,6).

Int tm_yday; / * number of days from January 1st this year after

the (0356).

Int tm_isdst; / * * / DST mark

};

If daylight saving time is valid, daylight saving time flag

tm_isdst value is positive; if daylight saving time is invalid,

tm_isdst value is 0; if daylight saving time information is not

obtained, tm_isdst value is negative.

The following time function is declared in the file ,

see the following table.

The time function declared in

Function definition

Function brief introduction

Clock_t clock (void)

Determine processor time function

Time_t time (time_t *tp)

Returns the current calendar time

Double difftime (time_t, time2, time_t, time1)

Calculates the time difference between two moments

Time_t mktime (struct, TM, *tp)

Converts the segmented time value to the calendar time value

Char *asctime (const, struct, TM, *tblock)

The date and time of the conversion are ASCII yards

Char *ctime (const, time_t, *time)

Converts date and time to strings

Struct, TM, *gmtime (const, time_t, *timer)

Change the date and time to the Greenwich standard time (GMT)

Struct, TM, *localtime (const, time_t, *timer)

Change the date and time to structure

Size_t strftime (char, *s, size_t, Smax, const, char, *fmt,

const, struct, TM, *tp)

According to the format requirements of FMT, the date and time

in *tp are converted to the specified format.


本文标签: 函数 定义 设置