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. 洛谷P1514 引水入城——dfs

    题目:https://www.luogu.org/problemnew/show/P1514 搜索+DP: 自己想出来的方法第一次80分好高兴! 再改了改就A了,狂喜乱舞: 也就是 dfs,仔细一想第 ...

  2. MySQL - MyCat 实现读写分离

    前言 MyCat是一个彻底开源的,面向企业应用开发的大数据库集群,支持事务.ACID.可以替代MySQL的加强版数据库.其功能有可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群.融 ...

  3. bzoj 1801: [Ahoi2009]chess 中国象棋【dp】

    注意到一行只能放012个炮,我们只需要知道列的状态,不用状压行 所以设f[i][j][k]表示前i行有j列有1个炮,有k列有2个炮的方案数 然后分情况讨论转移就行了 #include<cstdi ...

  4. 10.11NOIP模拟题(3)

    /* 可以看出,对于一段区间[L,R]如果统计了答案 若a[L]<a[R],那么当右端点往左移时答案不会更优,a[R]>a[L]同理 所以两个指针分别从头尾往中间扫那边小移哪边即可. */ ...

  5. CentOS 7 配置 Nginx 正向代理 http、https 最详解

    手头项目中有使用到 nginx,因为使用的三方云服务器,想上外网需要购买外网IP的,可是有些需要用到外网却不常用的主机也挂个外网IP有点浪费了,便想使用nginx的反向代理来实现多台内网服务器使用一台 ...

  6. GitHub安装使用教程

      由于重复率比较大,为了尊重他人的成果,先在此注明本文是在学习了以下博文之后的一些总结归纳,并且说明了一些本人实际使用GitHub遇到的问题,并给出了解决办法 .本人的操作系统是window10,所 ...

  7. spring简介及常用术语

    1.引入 在开发应用时常会遇到如下问题: 1)代码耦合性高: 2)对象之间依赖关系处理繁琐: 3)事务控制繁琐: 2.Spring简介 1)Spring概述 什么是Spring: ①Spring是一个 ...

  8. Dojo - 操作Dom的函数

    DOM Manipulation You might be seeing a trend here if you have gotten this far in the tutorial, in th ...

  9. droid开发:如何打开一个.dcm文件作为位图?

    我目前正在做一个Android应用程序的DICOM 继code打开图片DROM RES /绘制的“ussual”图像格式,但它不与.dcm工作 公共类BitmapView扩展视图 { 公共Bitmap ...

  10. spring加载classpath与classpath*的区别别

    1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...