08.C语言:特殊函数

C语言:特殊函数

1.递归函数:

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

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

 1 #include <stdio.h> 
 2 
 3 void fun(int n){
 4     printf("n = %d\n", n);
 5     n --;
 6     if(n > 0){
 7         fun(n);
 8     }
 9 }
10 
11 int main(int argc, char* argv[])
12 {
13     fun(5);
14     return 0;
15 }
recursionFunc

  

 

2.变参函数:

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

  1 STDARG(3)                                     Linux Programmer's Manual                                    STDARG(3)
  2 
  3 
  4 
  5 NAME
  6        stdarg, va_start, va_arg, va_end, va_copy - variable argument lists
  7 
  8 SYNOPSIS
  9        #include <stdarg.h>
 10 
 11        void va_start(va_list ap, last);
 12        type va_arg(va_list ap, type);
 13        void va_end(va_list ap);
 14        void va_copy(va_list dest, va_list src);
 15 
 16 DESCRIPTION
 17        A  function  may  be called with a varying number of arguments of varying types.  The include file <stdarg.h>
 18        declares a type va_list and defines three macros for stepping through a list of arguments  whose  number  and
 19        types are not known to the called function.
 20 
 21        The  called function must declare an object of type va_list which is used by the macros va_start(), va_arg(),
 22        and va_end().
 23 
 24    va_start()
 25        The va_start() macro initializes ap for subsequent use by va_arg() and va_end(), and must be called first.
 26 
 27        The argument last is the name of the last argument before the variable argument list, that is, the last argu‐
 28        ment of which the calling function knows the type.
 29 
 30        Because the address of this argument may be used in the va_start() macro, it should not be declared as a reg‐
 31        ister variable, or as a function or an array type.
 32 
 33    va_arg()
 34        The va_arg() macro expands to an expression that has the type and value of the next  argument  in  the  call.
 35        The  argument  ap is the va_list ap initialized by va_start().  Each call to va_arg() modifies ap so that the
 36        next call returns the next argument.  The argument type is a type name  specified  so  that  the  type  of  a
 37        pointer to an object that has the specified type can be obtained simply by adding a * to type.
 38 
 39        The first use of the va_arg() macro after that of the va_start() macro returns the argument after last.  Suc‐
 40        cessive invocations return the values of the remaining arguments.
 41 
 42        If there is no next argument, or if type is not compatible with the type of the actual next argument (as pro‐
 43        moted according to the default argument promotions), random errors will occur.
 44 
 45        If ap is passed to a function that uses va_arg(ap,type) then the value of ap is undefined after the return of
 46        that function.
 47 
 48    va_end()
 49        Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function.
 50        After  the  call va_end(ap) the variable ap is undefined.  Multiple traversals of the list, each bracketed by
 51        va_start() and va_end() are possible.  va_end() may be a macro or a function.
 52 
 53    va_copy()
 54        The va_copy() macro copies the (previously initialized) variable argument list src to dest.  The behavior  is
 55        as  if  va_start()  were applied to dest with the same last argument, followed by the same number of va_arg()
 56        invocations that was used to reach the current state of src.
 57 
 58        An obvious implementation would have a va_list be a pointer to the stack frame of the variadic function.   In
 59        such a setup (by far the most common) there seems nothing against an assignment
 60 
 61            va_list aq = ap;
 62 
 63        Unfortunately, there are also systems that make it an array of pointers (of length 1), and there one needs
 64 
 65            va_list aq;
 66            *aq = *ap;
 67 
 68        Finally,  on  systems where arguments are passed in registers, it may be necessary for va_start() to allocate
 69        memory, store the arguments there, and also an indication of which argument is next,  so  that  va_arg()  can
 70        step through the list.  Now va_end() can free the allocated memory again.  To accommodate this situation, C99
 71        adds a macro va_copy(), so that the above assignment can be replaced by
 72 
 73            va_list aq;
 74            va_copy(aq, ap);
 75            ...
 76            va_end(aq);
 77 
 78        Each invocation of va_copy() must be matched by a corresponding invocation of va_end() in the same  function.
 79        Some  systems  that do not supply va_copy() have __va_copy instead, since that was the name used in the draft
 80        proposal.
 81 
 82 CONFORMING TO
 83        The va_start(), va_arg(), and va_end() macros conform to C89.  C99 defines the va_copy() macro.
 84 
 85 NOTES
 86        These macros are not compatible with the historic macros they replace.  A backward-compatible version can  be
 87        found in the include file <varargs.h>.
 88 
 89        The historic setup is:
 90 
 91            #include <varargs.h>
 92 
 93            void
 94            foo(va_alist)
 95                va_dcl
 96            {
 97                va_list ap;
 98 
 99                va_start(ap);
100                while (...) {
101                    ...
102                    x = va_arg(ap, type);
103                    ...
104                }
105                va_end(ap);
106            }
107 
108        On  some systems, va_end contains a closing '}' matching a '{' in va_start, so that both macros must occur in
109        the same function, and in a way that allows this.
110 
111 BUGS
112        Unlike the varargs macros, the stdarg macros do not permit programmers to code a function with no fixed argu‐
113        ments.   This  problem generates work mainly when converting varargs code to stdarg code, but it also creates
114        difficulties for variadic functions that wish to pass all of their arguments on to a function  that  takes  a
115        va_list argument, such as vfprintf(3).
116 
117 EXAMPLE
118        The  function foo takes a string of format characters and prints out the argument associated with each format
119        character based on the type.
120 
121        #include <stdio.h>
122        #include <stdarg.h>
123 
124        void
125        foo(char *fmt, ...)
126        {
127            va_list ap;
128            int d;
129            char c, *s;
130 
131            va_start(ap, fmt);
132            while (*fmt)
133                switch (*fmt++) {
134                case 's':              /* string */
135                    s = va_arg(ap, char *);
136                    printf("string %s\n", s);
137                    break;
138                case 'd':              /* int */
139                    d = va_arg(ap, int);
140                    printf("int %d\n", d);
141                    break;
142                case 'c':              /* char */
143                    /* need a cast here since va_arg only
144                       takes fully promoted types */
145                    c = (char) va_arg(ap, int);
146                    printf("char %c\n", c);
147                    break;
148                }
149            va_end(ap);
150        }
151 
152 COLOPHON
153        This page is part of release 3.54 of the Linux man-pages project.  A description of the project, and informa‐
154        tion about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.
155 
156 
157 
158                                                      2013-03-15                                            STDARG(3)
stdarg
 1 /*******************************************************************
 2  *   > File Name: 02-variableFunc.c
 3  *   > Author: fly
 4  *   > Mail: XXXXXXXX@icode.com
 5  *   > Create Time: Sun 17 Sep 2017 09:44:31 AM CST
 6  ******************************************************************/
 7 
 8 #include <stdio.h>
 9 #include <stdarg.h>
10 
11 void myprint(int n, ...){
12     va_list p;
13     int i;
14 
15     va_start(p,n);
16 
17     for(i = 0; i < n; i++){
18         printf("%d\t", va_arg(p, int));
19     }
20     printf("\n");
21 
22     va_end(p);
23 }
24 
25 int main(int argc, char* argv[])
26 {
27     myprint(5, 10, 11, 12, 13, 14);
28     myprint(7, 10, 11, 12, 13, 14, 15, 16);
29 
30     return 0;
31 }
variableFunc.c

 

3.回调函数:

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

 1 /*******************************************************************
 2  *   > File Name: 03-callbackFunc.c
 3  *   > Author: fly
 4  *   > Mail: XXXXXXXX@icode.com
 5  *   > Create Time: Sun 17 Sep 2017 10:17:05 AM CST
 6  ******************************************************************/
 7 
 8 #include <stdio.h>
 9 
10 void callback_fun(void){
11     printf("%s :Hello world!\n", __FUNCTION__);
12 }
13 
14 void print(void(*p)(void)){
15     p();
16 }
17 
18 int main(int argc, char* argv[])
19 {
20     print(callback_fun);
21     return 0;
22 }
callback_fun.c

 

4.内联函数:

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

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

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

 

转载于:https://www.cnblogs.com/feige1314/p/7510185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值