scanf_m.c 439 B

123456789101112131415161718192021222324
  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;
  8. char s[6], c;
  9. /* Check that %[...]/%c work. */
  10. sscanf(buf, "%[a-z] %c", s, &c);
  11. /* Check that %m[...]/%mc work. */
  12. sscanf(buf, "%m[a-z] %mc", &ps, &pc);
  13. if (strcmp(ps, "hello") != 0 || *pc != 'w' ||
  14. strcmp(s, "hello") != 0 || c != 'w')
  15. return 1;
  16. free(ps);
  17. free(pc);
  18. return 0;
  19. }