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. bzoj4868

    http://www.lydsy.com/JudgeOnline/problem.php?id=4868 三分+贪心 我们可以知道这是一个单峰函数 当A>B那么我们每次调整一个的价钱是最佳的,所 ...

  2. 23. Ext xtype : "combo" 下拉选择框

    转自:https://blog.csdn.net/majishushu/article/details/52601161

  3. Flink源码阅读(1.7.2)

    目录 Client提交任务 flink的图结构 StreamGraph OptimizedPlan JobGraph ExecutionGraph flink部署与执行模型 Single Job Jo ...

  4. sql时间截取与修改

    --修改日,从1号修改到10号UPDATE CHECKINOUT SET CHECKTIME=DATEADD(DAY,9,CHECKTIME) WHERE YEAR(CHECKTIME)=1970 a ...

  5. 4-3 买家类目-service

    DAO:ProductCategory.Service可以简化一些,叫CategoryService. package com.imooc.sell.service; import com.imooc ...

  6. bzoj 1878: [SDOI2009]HH的项链【树状数组】

    对于一个lr,每个颜色贡献的是在(1,r)区间里出现的最右位置,所以记录一个b数组表示当前点这个颜色上一个出现的位置 然后把询问离线,按r升序排序 每次把右端点右移,把这个点在树状数组上+1,并且在当 ...

  7. bzoj 1637: [Usaco2007 Mar]Balanced Lineup【瞎搞】

    我是怎么想出来的-- 把种族为0的都变成-1,按位置x排升序之后,s[i]表示种族前缀和,想要取(l,r)的话就要\( s[r]-s[l-1]==0 s[r]==s[l-1] \),用一个map存每个 ...

  8. centos 扩展lrzsz通过xshell 下载安装文件

    yum自动安装: yum install lrzsz 手动安装方法如下: 定制安装的linux可能没有把rzsz包安装到系统,这对用securecrt这样的windows工具传输文件特别不方便.为了使 ...

  9. 清除WebSphere部署应用所对应的JSP缓存

    Web应用部署在WebSphere Application Server v8.5后程序一般放置在<AppServer>/profiles/<profile_name>/ins ...

  10. 百度地图API显示多个标注点带提示的代码 / 单个标注点带提示代码

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...