scanf_m.c 560 B

123456789101112131415161718192021222324252627
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6. const char *buf = "hello world";
  7. char *ps = NULL, *pc = NULL, *ps2 = NULL;
  8. char s[6], c, s2[5];
  9. /* Check that %[...]/%c/%s work. */
  10. sscanf(buf, "%[a-z] %c %s", s, &c, s2);
  11. /* Check that %m[...]/%mc/%ms work. */
  12. sscanf(buf, "%m[a-z] %mc %ms", &ps, &pc, &ps2);
  13. if (strcmp(ps, "hello") != 0 || *pc != 'w' ||
  14. strcmp(ps2, "orld") != 0 ||
  15. strcmp(s, "hello") != 0 || c != 'w' ||
  16. strcmp(s2, "orld") != 0)
  17. return 1;
  18. free(ps);
  19. free(pc);
  20. free(ps2);
  21. return 0;
  22. }