08.C语言:特殊函数
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语言:特殊函数的更多相关文章
- [08 Go语言基础-for循环]
[08 Go语言基础-for循环] 循环 循环语句是用来重复执行某一段代码. for 是 Go 语言唯一的循环语句.Go 语言中并没有其他语言比如 C 语言中的 while 和 do while 循环 ...
- 08. Go 语言包(package)
Go 语言包(package) Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同 ...
- Go语言特殊函数介绍
main 函数 Go语言程序的默认入口函数(主函数):func main()函数体用{}一对括号包裹.只能应用于package main func main(){ //函数体 } init 函数 go ...
- C语言特殊函数的应用
1. va_list相关函数的学习: va_list是一种变参量的指针类型定义. va_list使用方法如下: 1)首先在函数中定义一个具有va_list型的变量,这个变量是指向参数的指针. 2)首先 ...
- Go语言目录
为什么学习Go语言 第一章 环境搭建 Windows搭建Go语言环境 第二章 Go语言基础 Go语言介绍 Go语言命名 Go语言内置类型和函数 Go语言特殊函数介绍 Go语言运算符 第三章 Go语言程 ...
- C语言I作业12-学期总结
一.我学到的内容 二.我的收获 我完成的作业: 第一次作业 C语言I博客作业02 C语言I作业004 C语言I博客作业05 C语言I博客作业06 C语言I博客作业07 C语言I博客作业08 C语言I博 ...
- | C语言I作业12
C语言I作业12-学期总结 标签:18软件 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/S ...
- 读书笔记--SQL必知必会--Tips
01 - 如何获取SQL命令帮助信息 官方手册 help 或 help command MariaDB [(none)]> help General information about Mari ...
- C语言作业|08
问题 答案 这个作业的属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/9977 我 ...
随机推荐
- POJ3675 Telescope 圆和多边形的交
POJ3675 用三角剖分可以轻松搞定,数据也小 随便AC. #include<iostream> #include<stdio.h> #include<stdlib.h ...
- 0623-TP框架整理一(下载、入口文件、路由、创建控制器、调用模板、系统常量、命名空间)
一.下载解压后用ThinkPHP(核心)文件 核心文件夹(ThinkPHP)不要改,是作用于全局的,有需要可以改应用目录(Application) 二.创建入口文件: 运行后出现欢迎界面,在说明系统自 ...
- [App Store Connect帮助]五、管理构建版本(3)在您提交以供审核前选择构建版本
在提交 App 至“App 审核”前,请(从您为该版本上传的所有构建版本中)选择您想要提交的版本.一个 App Store 版本仅可关联一个构建版本.但是,在提交该版本至“App 审核”之前,您可以任 ...
- JPA中关联关系(OneToOne、OneToMany、ManyToMany,ManyToOne)映射代码片段
在使用Hibernate的时候我们常常会在类里边配置各种的关联关系,但是这个并不是很好配置,配置不当会出现各种各样的问题,下面具体来看一下: 首先我们来看User类里边有一个IdentityCard类 ...
- spring cloud config搭建说明例子(一)-简单示例
服务端 ConfigServer pom.xml添加config jar <dependency> <groupId>org.springframework.cloud< ...
- c++ memset函数
函数名称:memset 函数所需头文件:#include<cstring> 函数作用:内存赋值函数,用来给某一块内存空间进行赋值的. 函数结构:memset(变量,一个数字,一个数字) ...
- .Net MVC 与WebApi ActionFilterAttribute 区别
首先我们来看下 这两个ActionFilterAttribute 的命名空间区别的: 可以看出mvc 引用的是System.Web.Mvc,webapi 引用的是System.Web.Http.Fil ...
- 总结MySQL中SQL语法的使用
--where子句操作符: where子句操作符 = 等于 <> 不等于(标准语法) != 不等于(非标准语法,可移植性差) < 小于 <= 小于等于 > 大于 > ...
- web开发----jsp中通用模版的引用 include的用法
1.静态引入的示例 通过对两种用法的了解之后 我们现在 使用静态引入 因为上述原因 我的模版页中 只有div 不会有 path等定义 也不会有html标签 如下: 我的header.jsp 全 ...
- 8月中旬之后的学习计划 --- react
这段时间快活了,放纵了,玩hi了,接下来该好好的学习新知识了. 鉴于目前业界比较火的前端js框架有react.vue,决定先从react开始学习.之前有简单的接触过它的一些基本的语法知识,这次准备全面 ...