Question: While writing a arithmetic expression simplifier, I just observed this odd behavior. Recreated the problem which is below:

```
#include <stdio.h>

int main(void)
{
    int n, i;
    char s[200];

    for(i=0;i<2;i++){
        if(scanf("%d", &n) == 1)
        {
            printf("%d\n", n);
        } else {
            scanf("%s", s);
            printf("%s\n", s);
        }
    }
    return 0;
}

```

If the input is `5+10` it reads `5` in `n` and `10` in `s`. But, for other sign i.e `5-10` it read `5` in `n` and `-10` in `s` which was the expected output.

**My question is: Which C features caused this?** Checked on: gcc version 4.8.2 and ideone.

**EDIT**

---

Solved the problem with char by char reading of integers. Thanks to all for your time. `:)`

If anyone is interested: <http://ideone.com/rOPyQD>

Answer: 
> 
> If the input is 5+10 it reads 5 in n and 10 in s. But, for other sign i.e 5-10 it read 5 in n and -10 in s which was the expected output.
> 
> 
> 

You are mistaken, as you could determine by changing your `printf()` statements so that you can distinguish which one is generating each output. You will then find that your program is reading the `5` into `n` in the first iteration of the loop, and then reading either `10` or `-10` into *`n`*, not `s` on the second iteration.

This happens because the `%d` field descriptor reads an *optionally signed* decimal integer. "Optionally signed" means immediately preceded by *either* a `-` for negative *or a `+` for positive*. Thus, both `"+10"` and `"-10"` can be scanned via a `%d` field.
Here both are being read in 

```
if(scanf("%d", &n) == 1)

```

In first case, when you type 5+10, in first iteration 5 is being read in n and in second iteration +10 is being read in n. Since it is positive, +10 and -10 being same, it prints 10 in printf statement 

```
printf("%d\n", n);

```

And point to note here is that you are using "%d" in printf, not "%s". The printf statement with "%s" is not being invoked here.

This explains the second case also. -10 is being read to 'n' and printed using "%d"