睿宏 陳
PRO
last month
睿宏country asked

Can I add \n after the %d in scanf?

Palistha Singh
Expert
last month

Hi! You usually should not add \n after %d in scanf.

%d already skips leading whitespace (spaces, tabs, newlines). If you write scanf("%d\n", &x);, that \n tells scanf to keep waiting for more whitespace after the number, and it can look like the program is stuck until you press Enter again or type something else.

So use this:

scanf("%d", &ages[i]);

If you want each input on a new line, that’s handled by how the user types, not by adding \n to scanf.

C
This question was asked as part of the Learn C Programming course.