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. 4.7.5 Efficient Construction of LALR Parsing Tables

    4.7.5 Efficient Construction of LALR Parsing Tables There are several modifications we can make to A ...

  2. LD_LIBRARY_PATH设置问题

    今天突然遇到设置LD_LIBRARY_PATH的问题,,发现在.bashrc和/etc/profile中添加 exportLD_LIBRARY_PATH = path_name:$LD_LIBRARY ...

  3. PCB 批量Word转PDF实现方法

    自上次公司电脑中毒带来的影响,导致系统自动生成的Word档PCB出货报告,通过公司邮件服务器以附件的方式发送给客户后,客户是无法打开或打开缓慢的现象,如果将Word档转为PDF后在客户端是可以正常打开 ...

  4. Windows 和 Linux 上Redis的安装守护进程配置

    # Windows 和 Linux 上Redis的安装守护进程配置 Redis 简介 ​ Redis是目前最常用的非关系型数据库(NOSql)之一,常以Key-Value的形式存储.Redis读写速度 ...

  5. http升级https(转)

    让你的网站免费支持 HTTPS 及 Nginx 平滑升级 为什么要使用 HTTPS ? 首先来说一下 HTTP 与 HTTPS 协议的区别吧,他们的根本区别就是 HTTPS 在 HTTP 协议的基础上 ...

  6. JAVA的双色球 小程序

    还是挺简单的,功能过于强大. import java.util.Arrays; import java.util.Random; import java.util.Scanner; public cl ...

  7. 洛谷 P1064 金明的预算方案(有依赖的背包问题)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  8. ASP.NET MVC 导出CSV文件

    ASP.NET MVC   导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...

  9. [转]C语言/C++中如何产生随机数

    C语言/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,和time()函数. 需要说明的是,iostream头文件中就有srand函数的定义,不需要再额外引入stdlib. ...

  10. C. Coin Troubles 有依赖的背包 + 完全背包变形

    http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...