Behaviour of scanf when newline is in the format string

The '\n' character in the format string of scanf("%d\n", &paycode) matches any number of whitespace characters (space, tab, newline etc. – characters for which the isspace function declared in ctype.h yields true) in the input given. Therefore, the scanf call will read and discard any number of whitespace characters till it encounters a non-whitespace character at which point it will return. This is true for any whitespace character in the format string of scanf and not only the newline character. For example, the following will exhibit the same behaviour:

scanf("%d ", &paycode)
         ^ a space

You should change your scanf call to

scanf("%d", &paycode);

Also, instead of printf("%s", "Paycode: "); you can simply write printf("Paycode: ");
You have commented that stdio.h is a precompiled header. It’s not. It’s a header file which contains macro definitions and function declarations or prototypes. It’s not an object file in the sense of being precompiled.

Leave a Comment