我有个问题.我的课程中有一句话:
sprintf(buff,"Nieznany znak: %d",(char)va_arg(x, const char)); break;
为什么在编译后我有一个错误:
error.c: In function 'error':
error.c:30:46: warning: 'char' is promoted to 'int' when passed through '...' [e
nabled by default]
case 0xfe: sprintf(buff,"Nieznany znak: %d",(char)va_arg(x, const char)); brea
k;
^
error.c:30:46: note: (so you should pass 'int' not 'char' to 'va_arg')
error.c:30:46: note: if this code is reached, the program will abort
Run Code Online (Sandbox Code Playgroud)
在我看来一切都很好,为什么我不能这样做呢?
编译器告诉你原因:
'char' is promoted to 'int' when passed through '...'
Run Code Online (Sandbox Code Playgroud)
根据C语言规范,通常的算术转换应用于可变参数函数的未命名参数.因此,任何短于int(例如bool,char和short)的整数类型都被隐式转换int; float被提升为double等
执行编译器要求您执行的操作(va_arg()与int第二个参数一起使用),否则您的代码将调用未定义的行为.