C语言默认参数值的实现

http://blog.youkuaiyun.com/pipisorry/article/details/25437893

C语言中没有参数默认值的概念,可以利用宏来模拟参数默认值:

(对于有多个参数的函数,调用时需要将参数列表中的每个 " , "写出来,不过这样就可以把默认参数放在非默认参数之前)

以下代码在c下是不能通过的:

定义fun():

int fun(int a, int b = 10)                         ...                   ...

                                            ....     ...           ...     ....

     return a+b;                                  

                                                   ..                  ..

主函数代码:                                           ....        ....  

int main()                                                   ......

{

    printf("%d\n",fun(10));

    return 0;

}

..............................................................

编译时 fun(10) 调用会出错。注意这个代码在C++并不会错。

..............................................................

 

用宏改进后代码:

#include <stdio.h>
#define funi(a) fun( a, 10)
int fun(int a,int b )
{
        return a+b;

}
int main()
{
        int a = 10;
        printf("%d  %d\n",fun(a,10),funi(a));
        return 0;

}

结果: 20 20


[cpp]  view plain copy
  1. #include <stdio.h>  
  2.   
  3. #define DEFARG(name, defval) ((#name[0]) ? (name + 0) : defval)  
  4.   
  5. int _f1(int i)  
  6. {  
  7.     return 2*i;  
  8. }  
  9. #define f1(arg0) _f1(DEFARG(arg0, 0))  
  10.   
  11. int _f2(int i, int j)  
  12. {  
  13.     return i + j;  
  14. }  
  15. #define f2(arg0, arg1) _f2(DEFARG(arg0, 0), DEFARG(arg1, 1))  
  16.   
  17. int main()  
  18. {  
  19.     printf("%d\n", f1());  
  20.     printf("%d\n", f1(1));  
  21.   
  22.     printf("%d\n", f2(,));  
  23.     printf("%d\n", f2(2,));  
  24.     printf("%d\n", f2(, 3));  
  25.     printf("%d\n", f2(4, 5));  
  26.     return 0;  
  27. }  

#include <stdio.h>
#define DEFAULT 40      /*默认参数值*/
#define FUN(A) fun(#A##"-")    /*用于实现默认参数的宏*/

int f(int n)  /*用于实验默认参数的函数*/
{
 return printf("%d\n",n);
}
int fun(const char *a)    /*确定函数调用的函数,返回值类型要和实际需要调用的f()函数返回值类型一致*/
{
 int n; /*变量的类型要和f()函数参数的类型一样*/
 if (a[0]=='-') n=DEFAULT;
 else sscanf(a,"%d",&n);

 return f(n);

}
int main(void)
{
 FUN();
 FUN(67);
 return 0;
}

ps:
如果是有头文件的话:
getStr.h
#define getStr() _getStr( "TestFun.txt" );//set default filename in .c
void _getStr(char filename[]);
getStr.c
void _getStr(char filename[]){
...
}
main.c
#include "getStr.h"
void main(){
getStr();
}



ref: 
http://blog.youkuaiyun.com/broook/article/details/7208408
http://nonoob.is-programmer.com/posts/36769.html
http://www.myexception.cn/c/232391.html
http://wenku.baidu.com/view/1ffed5d86f1aff00bed51eea.html
http://blog.youkuaiyun.com/broook/article/details/7208408

from http://blog.youkuaiyun.com/pipisorry/article/details/25437893
C语言标准并不直接支持默认参数的功能,这与C++等语言不同。然而,可以通过一些技巧在C语言实现类似默认参数的行为。 ### 使用定义实现默认参数 一种常见的方法是使用定义来模拟默认参数。通过定义,可以在调用函数时根据是否传入参数提供默认值。 ```c #include <stdio.h> // 定义以支持默认参数 #define my_function(...) my_function_impl(__VA_ARGS__, 0) #define MAX_ARGS 2 void my_function_impl(int a, int b, ...) { printf("a = %d, b = %d\n", a, b); } int main() { my_function(10); // 使用默认值,输出 a = 10, b = 0 my_function(10, 20); // 指定所有参数,输出 a = 10, b = 20 return 0; } ``` ### 使用可变参数列表实现默认参数 另一种方法是利用C语言的可变参数列表(`stdarg.h`库)来处理不确定数量的参数,并手动指定默认值。 ```c #include <stdio.h> #include <stdarg.h> void my_function(int count, ...) { va_list args; va_start(args, count); int a = (count > 0) ? va_arg(args, int) : 0; // 默认值为0 int b = (count > 1) ? va_arg(args, int) : 0; // 默认值为0 printf("a = %d, b = %d\n", a, b); va_end(args); } int main() { my_function(1, 10); // 输出 a = 10, b = 0 my_function(2, 10, 20); // 输出 a = 10, b = 20 return 0; } ``` ### 使用结构体传递参数 还可以通过传递一个包含多个字段的结构体,并初始化未提供的字段为默认值,从而实现类似功能。 ```c #include <stdio.h> typedef struct { int a; int b; } Params; void my_function(Params p) { printf("a = %d, b = %d\n", p.a, p.b); } int main() { Params p = {10}; // 只设置a的值,b将被初始化为0 my_function(p); // 输出 a = 10, b = 0 return 0; } ``` 这些方法虽然不能完全替代C++中的默认参数机制,但可以有效地在C语言中模拟类似行为[^3]。 ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值