Variadic function (va_arg) doesn't work with float?

前端 未结 2 507
南方客
南方客 2020-12-01 16:34

I have a variadic function which takes a float parameter. Why doesn\'t it work?

va_arg(arg, float)
2条回答
  •  心在旅途
    2020-12-01 16:51

    As @dasblinkenlight has mentioned, float is promoted to double. It works fine for me:

    #include           
    #include 
    
    void foo(int n, ...)
    {   
        va_list vl;
        va_start(vl, n);
    
        int c; 
        double val; 
    
        for(c = 0; c < n; c++) {
            val = va_arg(vl, double);
            printf("%f\n", val);
        }
    
        va_end(vl);
    }
    
    
    int main(void)
    {
      foo(2, 3.3f, 4.4f);
      return 0;
    }
    

    Output:

    3.300000
    4.400000
    

提交回复
热议问题