sprintf.c 642 B

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