c中va_arg函数中的变量参数类型

我正在通过 gcc编译器收到警告,如果执行以下代码,程序将中止我无法理解为什么?如果有人澄清它会很有帮助.
#include<stdio.h>
#include<stdarg.h>
int f(char c,...);
int main()
{
   char c=97,d=98;
   f(c,d);
   return 0;
}

int f(char c,...)
{
   va_list li;
   va_start(li,c);
   char d=va_arg(li,char); 
   printf("%c\n",d);
   va_end(li);
}

海湾合作委员会告诉我:

warning: 'char’ is promoted to ‘int’ when passed through ‘...’ [enabled by default]
note: (so you should pass ‘int’ not ‘char’ to ‘va_arg’)
note: if this code is reached,the program will abort

解决方法

可变参数函数的参数经过认参数提升;小于int的任何东西(例如char)首先转换为int(并将float转换为double).

所以va_arg(li,char)永远不正确;请改用va_arg(li,int).

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...