Question: anyone knows an efficient way to check out the format of an scanf'ed data?

e.g. if I try to read an integer and I type a character, how would you do to tell the program that is not correct?

Answer: 
You can check if `scanf()` succeeds, it returns the number of successful conversions it performed.

You should *always* check this, before relying on the result since if it failed the variable(s) might contain undefined data leading to undefined results if referenced.

You can use `if` to check, and re-try with a different conversion specifier on failure:

```
if(scanf("%d", &x) == 1)
  printf("got integer %d\n", x);
else if(scanf("%c", &y) == 1)
  printf("got character '%c'\n", y);
else /* more attempts */

```

Of course it can become troublesome if there are "sub-matches", so the order can matter. It's also *way* better to split the input processing into two steps for the above:

1. Read a full line of input using `fgets()`
2. Use `sscanf()` to parse the line

That way you avoid problems due to the input being streamed in:

```
char line[128];

if(fgets(line, sizeof line, stdin) != NULL)
{
  int x;
  char y;

  if(sscanf(line, "%d", &x) == 1)
    printf("got integer %d\n", x);
  else if(sscanf(line, "%c", &y) == 1)
    printf("got character '%c'\n", y);
}

```

Note that if you wanted to scan for both an integer and a float, it can still become troublesome since a typical float (such as `"3.1416"`) begins with what is a legal integer. For those cases you can use the `strtoXXX()` family of functions, which let you check the remainder after doing the conversion.
```
Scanner sc = new Scanner (System.in);

try {

       // assume that the input from the user is not an integer, 
       // in that case the program cannot convert the input (which is a String) into
       // an integer. Because of this situation it'll jump to the 'catch' part of the 
       // program and execute the code. 

       int input = Integer.valueOf(sc.nextInt);

       // if the input is an integer lines below the above code will be executed. 

       // Ex. "int x = ( input + 10 ) "

}

catch (Exception ex) {

        System.out.println("Invalid input, please retry!");

        // if you want to get more information about 
        // the error use the 'ex' object as follows.

        System.out.println(ex);

}

```