What are the automatic type promotions of variadic function arguments?

后端 未结 3 1599
盖世英雄少女心
盖世英雄少女心 2020-12-11 06:06

Consider the following code snippet:

#include 
#include 

void display(int num, ...) {
    char c;
    int j;
    va_list ptr;         


        
3条回答
  •  轮回少年
    2020-12-11 06:18

    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

提交回复
热议问题