Variable argument type in va_arg function in c

I get a warning from the gcc compiler and the program aborts if the following code is executed. I could not understand why? It would be very helpful if someone clarified this.

#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); } 

GCC tells me the following:

 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 
+4
source share
2 answers

Arguments for variable functions go through the default advertising campaigns; anything smaller than int (e.g. char ) is first converted to int (and float converted to double ).

So va_arg(li,char) never correct; Use va_arg(li,int) instead.

+10
source

Yes, that seems like a fad in the C standard. However, it looks like it's only related to va_arg() .

You can take a look at the various implementations of printf() to see how to overcome this. For example, one in klibc is pretty easy to read.

+1
source

All Articles