Question: I have this scanf code line:

```
scanf("%d,%d %d,%d", &X, &Y, &dirX, &dirY);

```

How can I check if the input, which should be for example `1,2 3,4` (2 commas) is indeed written like this, and only like this?

I don't want to accept `1,2,3,4` (3 commas) for example.

Answer: 
What you can do is check the return value of `scanf()` to see whether the user has entered the input is the proper format or not. Like

```
int main (void)
{
    int i, j, k, l;

    if(scanf("%d,%d %d,%d", &i, &j, &k, &l) != 4)
        printf("User entered input in wrong format\n");
        printf("please enter in the format i,j k,l");
        //exit here
    else
        printf("%d %d %d %d", i, j, k, l);
}

```

---

This uses the fact that if user enters input in some other format (other then 1,2 3,4) then `scanf()` will not be able to read all the `4` inputs, and thus will not return `4`.
If you really need exact input checking, why not to use just `scanf("%s", &s)` (just string) and then `strncmp(3)`?

Formatted input is just about format, not semantics. So option is to put on `scanf` only general format validation like field width, field type (digits or alphanumeric), delimiters and so on. Semantics check goes onto different level.

If you need serious check of input like both format and semantics, I'd like to recommend tools like [flex](http://linux.die.net/man/1/bison) and [bison](http://linux.die.net/man/1/bison). They require more attention but they are MUCH more powerful.

Speaking in other words you definitely need something like:

```
int main (int argc, char **argv)
{
    int num. i, j, k, l;

    // Check format.
    num = scanf("%d,%d %d,%d", &i, &j, &k, &l);
    assert(num == 4);

    // Check semantics.
    assert((i == 1) && (j ==2) && (k == 3) && (l == 4));
}  

```