默认参数提升在可变参数函数的陷阱

604 篇文章 8 订阅
579 篇文章 5 订阅

如果明白了C语言的可变参数函数,让我们实现一个简易的my_printf
1. 它只返回void, 不记录输出的字符数目
2. 它只接受"%d"按整数输出、"%c"按字符输出、"%%"输出'%'本身
很多人的答案如下:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stdarg.h>  
  4.   
  5. void my_printf(const char* fmt, ... )  
  6. {  
  7.     va_list ap;  
  8.     va_start(ap,fmt); /* 用最后一个具有参数的类型的参数去初始化ap */  
  9.     for (;*fmt;++fmt)  
  10.     {  
  11.         /* 如果不是控制字符 */  
  12.         if (*fmt!='%')  
  13.         {  
  14.             putchar(*fmt); /* 直接输出 */  
  15.             continue;  
  16.         }  
  17.   
  18.         /* 如果是控制字符,查看下一字符 */  
  19.         ++fmt;  
  20.         if ('\0'==*fmt) /* 如果是结束符 */  
  21.         {  
  22.             assert(0);  /* 这是一个错误 */  
  23.             break;  
  24.         }  
  25.   
  26.         switch (*fmt)  
  27.         {  
  28.             case '%'/* 连续2个'%'输出1个'%' */  
  29.                 putchar('%');  
  30.                 break;  
  31.             case 'd'/* 按照int输出 */  
  32.             {  
  33.                 /* 下一个参数是int,取出 */  
  34.                 int i = va_arg(ap,int);  
  35.                 printf("%d",i);  
  36.             }  
  37.             break;  
  38.             case 'c'/* 按照字符输出 */  
  39.             {  
  40.                 /** 但是,下一个参数是char吗*/  
  41.                 /*  可以这样取出吗? */  
  42.                 char c = va_arg(ap,char);  
  43.                 printf("%c",c);  
  44.             }  
  45.             break;  
  46.         }  
  47.     }  
  48.     va_end(ap);  /* 释放ap—— 必须! 见下文分析*/  
  49. }  

很可惜,这样的代码是错误的!

简单的说,我们用va_arg(ap,type)取出一个参数的时候,
type绝对不能为以下类型:
——charsigned char、unsigned char
——short、unsigned short
——signed shortshort int、signed short int、unsigned short int
——float


一个简单的理由是:
——调用者绝对不会向my_printf传递以上类型的实际参数。

为什么呢?-- 这里就牵扯到默认参数提升问题。

看标准:

If the expression that denotes the called function has a type that does include a prototype, the  arguments  are  implicitly  converted,  as  if  by  assignment,  to the types of  the corresponding parameters, taking the type of each parameter to be the unqualied versionof  its  declared  type. The  ellipsis notation in a function prototype declarator  causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments. -- C11 6.5.2.2  Function calls (7)

C语言中什么时候会牵扯到默认参数提升呢?

在C语言中,调用一个不带原型声明的函数时:调用者会对每个参数执行“默认实际参数提升(default argument promotions)。

同时,对可变长参数列表超出最后一个有类型声明的形式参数之后的每一个实际参数,也将执行上述提升工作。

提升工作如下:
——float类型的实际参数将提升到double
——charshort和相应的signedunsigned类型的实际参数提升到int
——如果int不能存储原值,则提升到unsigned int

 

然后,调用者将提升后的参数传递给被调用者。
所以,my_printf是绝对无法接收到上述类型的实际参数的。

上面的代码的42与43行,应该改为:
int c = va_arg(ap,int);
printf("%c",c);

同理, 如果需要使用short和float, 也应该这样:
short s = (short)va_arg(ap,int);
float f = (float)va_arg(ap,double);

----------------------------------------------------------------------我是分割线--------------------------------------------------------------------

    C标准对默认实际参数提升规则有明确定。
也就是说, 带有可变长参数列表的函数, 绝对不会接受到char类型的实际参数。

    C标准对va_arg是否自动对齐没有任何说明

也就是说自动对齐工作,编译器可做可不做。

在所有C实现上,能保证第 点,不能保证第 点,所以尽管编译器实现了自动对齐,也要按标准来。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值