How I make the following code work? I want to print “non defined” instead of -1.#IND00
int myprint(const char* format, ...)
{
va_list args;
va_start (args, format);
int ret;
if(_isnan(static_cast<float>(*args)))
ret = printf ("non defined");
else
ret = vprintf (format, args);
fflush(stdout);
va_end (args);
return ret;
}
int main()
{
myprint("%f", sqrt(-1.0));
return 0;
}
You can’t use
argslike that, you have to useva_argto get an actual argument.would do the trick, but that won’t help you much. You can’t infer the type from the arguments. The type you indicate to
va_argmust be the actual type of the object passed in.And with that, your
vprintfcall won’t work either, you need to “re-start” the va_list sinceva_arghas “consumed” one argument already.