C语言:特殊函数

1.递归函数:

  与普通函数比较,执行过程不同,该函数内部调用它自己,它的执行必须要经过两个阶段:递推阶段,回归阶段;

  当不满足回归条件,不再递推;

 #include <stdio.h> 

 void fun(int n){
printf("n = %d\n", n);
n --;
if(n > ){
fun(n);
}
} int main(int argc, char* argv[])
{
fun();
return ;
}

recursionFunc

  

2.变参函数:

  与普通函数比较,定义形式不同,例如:int printf(const char *format, ...);

 STDARG()                                     Linux Programmer's Manual                                    STDARG(3)

 NAME
stdarg, va_start, va_arg, va_end, va_copy - variable argument lists SYNOPSIS
#include <stdarg.h> void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src); DESCRIPTION
A function may be called with a varying number of arguments of varying types. The include file <stdarg.h>
declares a type va_list and defines three macros for stepping through a list of arguments whose number and
types are not known to the called function. The called function must declare an object of type va_list which is used by the macros va_start(), va_arg(),
and va_end(). va_start()
The va_start() macro initializes ap for subsequent use by va_arg() and va_end(), and must be called first. The argument last is the name of the last argument before the variable argument list, that is, the last argu‐
ment of which the calling function knows the type. Because the address of this argument may be used in the va_start() macro, it should not be declared as a reg‐
ister variable, or as a function or an array type. va_arg()
The va_arg() macro expands to an expression that has the type and value of the next argument in the call.
The argument ap is the va_list ap initialized by va_start(). Each call to va_arg() modifies ap so that the
next call returns the next argument. The argument type is a type name specified so that the type of a
pointer to an object that has the specified type can be obtained simply by adding a * to type. The first use of the va_arg() macro after that of the va_start() macro returns the argument after last. Suc‐
cessive invocations return the values of the remaining arguments. If there is no next argument, or if type is not compatible with the type of the actual next argument (as pro‐
moted according to the default argument promotions), random errors will occur. If ap is passed to a function that uses va_arg(ap,type) then the value of ap is undefined after the return of
that function. va_end()
Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function.
After the call va_end(ap) the variable ap is undefined. Multiple traversals of the list, each bracketed by
va_start() and va_end() are possible. va_end() may be a macro or a function. va_copy()
The va_copy() macro copies the (previously initialized) variable argument list src to dest. The behavior is
as if va_start() were applied to dest with the same last argument, followed by the same number of va_arg()
invocations that was used to reach the current state of src. An obvious implementation would have a va_list be a pointer to the stack frame of the variadic function. In
such a setup (by far the most common) there seems nothing against an assignment va_list aq = ap; Unfortunately, there are also systems that make it an array of pointers (of length ), and there one needs va_list aq;
*aq = *ap; Finally, on systems where arguments are passed in registers, it may be necessary for va_start() to allocate
memory, store the arguments there, and also an indication of which argument is next, so that va_arg() can
step through the list. Now va_end() can free the allocated memory again. To accommodate this situation, C99
adds a macro va_copy(), so that the above assignment can be replaced by va_list aq;
va_copy(aq, ap);
...
va_end(aq); Each invocation of va_copy() must be matched by a corresponding invocation of va_end() in the same function.
Some systems that do not supply va_copy() have __va_copy instead, since that was the name used in the draft
proposal. CONFORMING TO
The va_start(), va_arg(), and va_end() macros conform to C89. C99 defines the va_copy() macro. NOTES
These macros are not compatible with the historic macros they replace. A backward-compatible version can be
found in the include file <varargs.h>. The historic setup is: #include <varargs.h> void
foo(va_alist)
va_dcl
{
va_list ap; va_start(ap);
while (...) {
...
x = va_arg(ap, type);
...
}
va_end(ap);
} On some systems, va_end contains a closing '}' matching a '{' in va_start, so that both macros must occur in
the same function, and in a way that allows this. BUGS
Unlike the varargs macros, the stdarg macros do not permit programmers to code a function with no fixed argu‐
ments. This problem generates work mainly when converting varargs code to stdarg code, but it also creates
difficulties for variadic functions that wish to pass all of their arguments on to a function that takes a
va_list argument, such as vfprintf(). EXAMPLE
The function foo takes a string of format characters and prints out the argument associated with each format
character based on the type. #include <stdio.h>
#include <stdarg.h> void
foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s; va_start(ap, fmt);
while (*fmt)
switch (*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
} COLOPHON
This page is part of release 3.54 of the Linux man-pages project. A description of the project, and informa‐
tion about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. -- STDARG()

stdarg

 /*******************************************************************
* > File Name: 02-variableFunc.c
* > Author: fly
* > Mail: XXXXXXXX@icode.com
* > Create Time: Sun 17 Sep 2017 09:44:31 AM CST
******************************************************************/ #include <stdio.h>
#include <stdarg.h> void myprint(int n, ...){
va_list p;
int i; va_start(p,n); for(i = ; i < n; i++){
printf("%d\t", va_arg(p, int));
}
printf("\n"); va_end(p);
} int main(int argc, char* argv[])
{
myprint(, , , , , );
myprint(, , , , , , , ); return ;
}

variableFunc.c

3.回调函数:

  与普通函数比较,调用过程不同,所谓的回调函数,指的是不直接在程序中显式的调用,而是通过调用其他函数返回调用的函数。

 /*******************************************************************
* > File Name: 03-callbackFunc.c
* > Author: fly
* > Mail: XXXXXXXX@icode.com
* > Create Time: Sun 17 Sep 2017 10:17:05 AM CST
******************************************************************/ #include <stdio.h> void callback_fun(void){
printf("%s :Hello world!\n", __FUNCTION__);
} void print(void(*p)(void)){
p();
} int main(int argc, char* argv[])
{
print(callback_fun);
return ;
}

callback_fun.c

4.内联函数:

  与普通函数比较,调用过程不同,定义的位置不同,例如:

    1》、调用为复制的过程;

    2》、结合了普通函数和带参宏的优点的一种函数。

08.C语言:特殊函数的更多相关文章

  1. [08 Go语言基础-for循环]

    [08 Go语言基础-for循环] 循环 循环语句是用来重复执行某一段代码. for 是 Go 语言唯一的循环语句.Go 语言中并没有其他语言比如 C 语言中的 while 和 do while 循环 ...

  2. 08. Go 语言包(package)

    Go 语言包(package) Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同 ...

  3. Go语言特殊函数介绍

    main 函数 Go语言程序的默认入口函数(主函数):func main()函数体用{}一对括号包裹.只能应用于package main func main(){ //函数体 } init 函数 go ...

  4. C语言特殊函数的应用

    1. va_list相关函数的学习: va_list是一种变参量的指针类型定义. va_list使用方法如下: 1)首先在函数中定义一个具有va_list型的变量,这个变量是指向参数的指针. 2)首先 ...

  5. Go语言目录

    为什么学习Go语言 第一章 环境搭建 Windows搭建Go语言环境 第二章 Go语言基础 Go语言介绍 Go语言命名 Go语言内置类型和函数 Go语言特殊函数介绍 Go语言运算符 第三章 Go语言程 ...

  6. C语言I作业12-学期总结

    一.我学到的内容 二.我的收获 我完成的作业: 第一次作业 C语言I博客作业02 C语言I作业004 C语言I博客作业05 C语言I博客作业06 C语言I博客作业07 C语言I博客作业08 C语言I博 ...

  7. | C语言I作业12

    C语言I作业12-学期总结 标签:18软件 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/S ...

  8. 读书笔记--SQL必知必会--Tips

    01 - 如何获取SQL命令帮助信息 官方手册 help 或 help command MariaDB [(none)]> help General information about Mari ...

  9. C语言作业|08

    问题 答案 这个作业的属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/9977 我 ...

随机推荐

  1. bzoj3262 陌上花开——CDQ分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3262 第一道CDQ分治题! 看博客:https://www.cnblogs.com/Narh ...

  2. vue watcher

    观察 Watchers 虽然计算属性在大多数情况下更合适,但有时也需要一个自定义的 watcher .这是为什么 Vue 提供一个更通用的方法通过watch 选项,来响应数据的变化.当你想要在数据变化 ...

  3. Python基础 — Matplotlib

    Matplotlib -- 简介         matplotlib是Python优秀的数据可视化第三方库:        matplotlib库的效果可参考官网:http://matplotlib ...

  4. robotframework - create dictionary 操作

    1.创建字典 2.从字典中获取的项 -- 打印出 item 3.获取字典的key -- 打印出 key 4.获取字典的value -- 打印出 value 5.获取字典key,value 6.打印出字 ...

  5. (DP)51NOD 1085 背包问题

    在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...

  6. MySQL故障处理一例_Another MySQL daemon already running with the same unix socket

    MySQL故障处理一例:"Another MySQL daemon already running with the same unix socket". [root@test- ...

  7. CodeDOMProvider 类

    CodeDomProvider 可用来创建和检索代码生成器和代码编译器的实例.代码生成器可以生成特定语言的代码,如:C#.Visual Basic.JScript 等,而代码编译器可以将代码文件编译成 ...

  8. 全面学习ORACLE Scheduler特性(1)创建jobs

    所谓出于job而胜于job,说的就是Oracle 10g后的新特性Scheduler啦.在10g环境中,ORACLE建议使用Scheduler替换普通的job,来管理任务的执行.其实,将Schedul ...

  9. Hive insert into directory 命令输出的文件没有列分隔符分析和解决

    参考资料:http://stackoverflow.com/questions/16459790/hive-insert-overwrite-directory-command-output-is-n ...

  10. 连接oracle出现的问题以及解决办法

    连接oracle出现过的问题: 1,ORA-12514::监听程序当前无法识别链接描述符中请求的服务 1)重启服务,看是否解决 2)测试网络监听是否能监听成功,监听不成功的话,查看下面几个点:服务名( ...