Okay so maybe inputing ints isn't so bad, but it still chokes on chars pretty easily:
Code:
#include <stdlib.h>
void test(const char* format)
{
printf("Format: %s\n", format);
char a, b, c, d;
puts("Enter A and B");
scanf(format, &a, &b);
printf("A:%c B:%c\n", a, b);
puts("Enter C and D");
scanf(format, &c, &d);
printf("C:%c D:%c\n", c, d);
}
int main()
{
test("%c%c%*c");
test("%c%c);
}
The "%*c" bit seems to do the trick here.
But then you still have the problem where if the user enters bad input things could get wonky. If you really want good control over your input stream maybe a better approach would be to use fgets with sscanf.
Code:
#include <stdlib.h>
#define MAX 1024
void test(const char* format)
{
char input[MAX];
printf("Format: %s\n", format);
char a, b, c, d;
puts("Enter A and B");
fgets(input, MAX, stdin);
sscanf(input, format, &a, &b);
printf("A:%c B:%c\n", a, b);
puts("Enter C and D");
fgets(input, MAX, stdin);
sscanf(input, format, &c, &d);
printf("C:%c D:%c\n", c, d);
}
int main()
{
test("%c%c%*c");
test("%c%c");
}