ISO/IEC 9899:1999 says
sprintf returns -1 if the output is truncated, and MSVC does this. So if I need portability, I can't use
sprintf's return value.
sprintf is not the same as snprintf.
snprintf is one of the most portable ways to do this, as long as the implementation followed the standard.
There is another way. On windows, you can write anything to NUL, a bit bucket, and the operation will be successful, but the input will be discarded. The Linux version of this is called "/dev/null". I don't know if other operating systems provide this (they certainly don't have to). If what I mentioned is all you care about though, that's portable enough.
Code:
long int printed = 0;
FILE *fnul = fopen(onlinux() ? "/dev/null" : "NUL", "w");
if (fnul != NULL) {
printed = fprintf(fnul, "Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer);
fclose(fnul);
}
In any case, I wonder what you need this information for, because as far as I know, the only time printf fails to write is if stdout isn't open. Other problems with printf and its arguments will be caught at compile time. Is stdout's state really a concern for you, and why?