0
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
 
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 1
    The space before `%c` makes `scanf` remove and discard all white spaces in the input buffer, i.e. `ch` will be assigned the value of the first non-white space character – Support Ukraine May 10 '21 at 15:04
  • 1
    To make it clear, the space in the format string is an attempt to solve the problem described in the linked question. It works only when this is not the first thing the user enters. In your case, it's the first thing, so it requires the user to input a space. So ugly. – anatolyg May 10 '21 at 15:18
  • @anatolyg What do you mean by: "so it requires the user to input a space." ? – Support Ukraine May 10 '21 at 15:34
  • The `scanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. It will be read by the *next* `scanf()`. Format specifiers `%d` and `%s` and `%f` automatically filter such leading whitespace characters, but `%c` and `%[]` and `%n` do not. In these three cases you can instruct `scanf` to do so by adding a space just before the `%`, but for other specifiers it isn't necessary. – Weather Vane May 10 '21 at 16:37
  • thanks for your clear explaination man – Raghul- M May 11 '21 at 10:38

0 Answers0