When using the scanf function to take an input, two inputs are given.
For starters, I am a python programmer trying to learn C. I tried to search why this was happening but found nothing. Here is the code:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d\n", &age);
printf("Your age is %d\n", age);
return 0;
}
When running and debugging in VSCode, this is what I get:
Enter your age: 23
12
Your age is 23
Any help would be appreciated.
>Solution :
The \n is unnecessary in the scanf format string. It tells scanf to continue reading any number of whitespace characters, until it finds the next non-whitespace character.
Instead, just use scanf("%d", &age);