0

I have a simple code:

#include <stdio.h>

int main(void) {
    int alter;
    printf("Your age: ");
    scanf("%d\n", &alter);
    printf("your age is %d\n", alter);
}

Output is:

Your age: <entering some_number(1)> (enter)

(terminal is waiting for another value)

<entering some_number(2)> (enter)

your age is number(1)

why does it wait for me to enter 2 values?

thanks

rioV8
  • 24,506
  • 3
  • 32
  • 49
atelten
  • 13
  • 2
  • what is the reason of the `\n` in the scanf format string – rioV8 Aug 30 '21 at 10:14
  • The incorrect `\n` in the `scanf` format string has to filter *any amount* of subsequent whitespace. So it is not until it receives a non-whitespace character (which will remain in the input buffer) that `scanf` can complete and return. Note that `scanf` does resemble `printf` but is very different in its behaviour. – Weather Vane Aug 30 '21 at 10:17

1 Answers1

0

There should be be \n in the scanf. It should be

scanf("%d", &alter);

starboy_jb
  • 899
  • 1
  • 6
  • 13