scanf.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. struct params {
  5. short sa;
  6. int ia;
  7. int ib;
  8. int ic;
  9. float fa;
  10. double da;
  11. int *ptr;
  12. char c;
  13. char string[20];
  14. };
  15. void test(char* fmt_in, char* input, struct params *p, ...) {
  16. va_list args;
  17. va_start(args, p);
  18. int ret = vsscanf(input, fmt_in, args);
  19. va_end(args);
  20. printf(
  21. "%d, { sa: %hhd, ia: %d, ib: %d, ic: %d, fa: %f, da: %lf, ptr: %p, char: %c, string: %s }\n",
  22. ret, p->sa, p->ia, p->ib, p->ic, p->fa, p->da, p->ptr, p->c, p->string
  23. );
  24. }
  25. int main(void) {
  26. struct params p = { .c = 'a' };
  27. test("%hhd %d", "12 345", &p, &p.sa, &p.ia);
  28. test("%x %i %i", "12 0x345 010", &p, &p.ia, &p.ib, &p.ic);
  29. test("%f.%lf", "0.1.0.2", &p, &p.fa, &p.da);
  30. test("%p", "0xABCDEF", &p, &p.ptr);
  31. test("%s", "Hello World", &p, &p.string);
  32. test("%3i", "0xFF", &p, &p.ia);
  33. test("%c%3c", "hello", &p, &p.c, &p.string);
  34. test("test: %2i%n", "test: 0xFF", &p, &p.ia, &p.ib);
  35. test("hello world%%", "hello world%", &p);
  36. }