sprintf.c 567 B

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