There are several solutions, but none is entirely trivial. Unfortunately, what you are finding is that SOME systems have a "catch" for NULL pointers in the handling of "%s", which fixes up the problem you are seeing. This is not required by the C standard.
The most simple (in terms of complexity of implementation) is to simply check the string before passing it into example
, e.g. :
char *FileName = NULL;
if (!FileName) FileName = "(null)");
ret = example(buff,100,"File Name is %s",FileName);
or
char *FileName = NULL;
ret = example(buff,100,"File Name is %s",FileName?FileName:"(null)");
But if example
is called a gazillion times in the code, it may be simpler to search for "%s" and check at that point if the value is NULL, and replace it. This gets quite messy, because you pretty much have to implement a full "%[flags][width][type]" parser for the printf formatting, and walk over the arguments. But there is a further problem here: You can't modify the va_list
(it MAY work, but it's undefined behaviour), so you probably will end up writing your own vsnprintf
function.
So, I would try to fix it at the source before going down the route of implementing your own function.