Last active
February 9, 2024 21:46
-
-
Save DavidEGrayson/6ce6dc23ad49c8ecfdd3c2b63a80410a to your computer and use it in GitHub Desktop.
Quick test of vsnprintf to make sure it is standards-compliant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a small C program that tests your version of vsnprintf to see if it is | |
// compliant with the C99 standard. | |
// | |
// The script gives successful results (compliant vsnprintf) on these environments: | |
// - MSYS2's MINGW64 environment (GCC 13.2.0) | |
// | |
// I also expect successful results on Linux, macOS and modern UCRT-based environments | |
// provided by Microsoft. | |
// | |
// From the MSDN documentation of vsnprintf: | |
// Beginning with the UCRT in Visual Studio 2015 and Windows 10, vsnprintf is no longer | |
// identical to _vsnprintf. The vsnprintf function conforms to the C99 standard; | |
// _vsnprintf is kept for backward compatibility with older code. The difference is | |
// that if you run out of buffer, vsnprintf null-terminates the end of the buffer | |
// and returns the number of characters that would have been required, while _vsnprintf | |
// doesn't null-terminate the buffer and returns -1. Also, _vsnprintf() includes one | |
// more character in the output because it doesn't null-terminate the buffer. | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdarg.h> | |
int main() { | |
va_list ap; | |
char s[4] = {}; | |
int r = vsnprintf(s, 2, "abcd", ap); | |
printf("%d %s\n", r, s); // should be "4 a" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment