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

Dha*_*tri 6 c

我正在通过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);
}
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会告诉我:

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
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 11

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

所以va_arg(li,char)永远不正确; 使用va_arg(li,int)来代替.