What are the automatic type promotions of variadic function arguments?

不羁的心 提交于 2019-11-27 07:55:40

问题


Consider the following code snippet:

#include <stdio.h>
#include <stdarg.h>

void display(int num, ...) {
    char c;
    int j;
    va_list ptr;
    va_start(ptr,num);
    for (j= 1; j <= num; j++){
        c = va_arg(ptr, char);
        printf("%c", c);

    }
    va_end(ptr);
}

int main() {
    display(4, 'A', 'a', 'b', 'c');
    return 0;
}

The program gives runtime error because vararg automatically promotes char to int, and i should have used int in this case.

What are all types are permitted when I use vararg, how to know which type to use and avoid such runtime errors.


回答1:


another case that the others forgot to mention are pointer types, critical is NULL in particular. Since this could expand to 0 or (void*)0 (or some other weird things) you will not know if the compiler puts an int or a void* in the list. Since these can have different width, this can lead to annoying bugs.




回答2:


You can use any standard type with va_arg except char, signed char, unsigned char, short, unsigned short, _Bool, and float. It's possible that an implementation defines additional nonstandard types that also have integer conversion rank lower than int, or likewise nonstandard small floating-point types, but you would not need to be aware of these unless you intend to use them, so for practical purposes the list I gave is complete.




回答3:


While using va_arg the char is promoted to int. There are other types(the list given by @R..) which are promoted.

so in order to read it as a char you have to do typecast.

like: c = (char) va_arg(ap, int);

for the complete example please see:

http://en.cppreference.com/w/cpp/language/variadic_arguments



来源:https://stackoverflow.com/questions/7084857/what-are-the-automatic-type-promotions-of-variadic-function-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!