Chapter 9 Functions Revisited

Function pointer

// 指定返回值和参数
int (*p)(int i);
p(1);
// 可以使用函数名赋值
p = fun;
// 函数指针 数组
int (*p[10])(int i);
// 初始化 多个函数名
int (*p[10])(int i)={a, b, c};
// 省略维度的初始化
int (*p[])(int i)={a, b, c};

Function pointer as a parameter

Function variables

Automatic variables

Automatic creation and destruction

Static variables

static
exit function variables are not initialized only once destroyed
default initialized to 0
only visible statement block statement

Global Variables

Initialized to 0 by default
statement outside the function can be used for all functions

Static automatic variables are hidden global variables

Recursion

The variable parameter function

stdarg.h

Function prototype

int test(int i, int j, ...);

At least one fixed parameter

Macro acquisition parameters

va_list
va_start ()
va_arg ()
va_end ()
va_copy () to copy the remaining parameters
do not use it until a backup copy to release another list
to ensure proper circulation at the end of the last incoming 0 or NULL

Variable parameter function requires

  1. At least one fixed argument
  2. Adding value to ensure the normal cycle ends Sentinel
  3. the va_arg () can get pointer value
  4. Must va_end ()

main () function parameters

// argc 参数个数+1 包含程序名
// argv 字符串形式的参数
// 含空格的参数 要使用双引号引起来
int main(int argc, char * argv[])

End of program

stdlib.h

abort()

No abnormal end parameter has no value is returned
empty to close the open flow output buffer

exit(int)

Ends normally
empty output buffer to close the open flow
0 EXIT_SUCCESS successful conclusion
EXIT_FAILURE end failure

atexit()

Sign exit () function call successfully returns 0
up to 32 function
reverse call FILO

_Exit()

With exit () function does not call register

quick_exit(int)

Normal end
call _Exit (int)

at_quick_exit()

Functions registered quick_exit () called
reverse call FILO

exit () and between quick_exit () function independently of each other independent register

Improve performance

inline

Tips for the compiler does not guarantee the effect of
calling compiled short way function
declared inline function

inline int add(int i, int j){
	return i + j;
}

restrict

Hint to the compiler does not secure the effect of
the pointer without changing calculation order alias compilers optimize the code

char * strcpy_s(char * restrict str1, char * restrict str2);

_Noreturn

Never returned
compiler omitted it back to the calling code and the need to control the front storage space
stdnoreturn.h noreturn macro _Noreturn

_Noreturn void finish(){
	exit(EXIT_SUCCESS);
}
Published 17 original articles · won praise 1 · views 301

Guess you like

Origin blog.csdn.net/yuanshou1/article/details/104795214