sprintf.c 682 B

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