I have written the below small code with several conditions on number entered by the user
#include<stdio.h>
int main()
{
    int userInput;
    while(1)
    {
        printf("Press '1':To Print\n");
        scanf("%d",&userInput);
        if(userInput==1)
        {
            printf("ABCD\n");
        }
        else
        {
            printf("WARNING:Pressed invalid key");
        }
    }
}
When I press any number then it works fine, but when I press up arrow/any character key and press enter the loop runs infinite times.
Why is this happening and is there any way to prevent it?
 
     
     
    