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.
display(4, 'A', 'a', 'b', 'c');there are 5ints; not 1intand 4chars as you seem to believe :)va_arg(ptr, char), not the call to thedisplayfunction -- where there are nochars :)wchar_talso doesn't work, and neither dochar16_torchar32_t(they're all promoted tounsigned int).