2
0

sprintf.c 707 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. char buffer[72];
  7. int ret = sprintf(
  8. buffer,
  9. "This string fits in the buffer because it is only %d bytes in length",
  10. 68
  11. );
  12. if (ret != 68) {
  13. printf("Failed! Return value was %d\n", ret);
  14. exit(EXIT_FAILURE);
  15. }
  16. memset(buffer, 0, sizeof(buffer));
  17. ret = snprintf(
  18. buffer,
  19. 10,
  20. "This string is way longer and does not fit in the buffer because it %d bytes in length",
  21. 86
  22. );
  23. if (ret != 86) {
  24. printf("Failed! Return value was %d\n", ret);
  25. exit(EXIT_FAILURE);
  26. }
  27. puts(buffer);
  28. }