With C programming language,
I'm trying to read and store a string input to a char array which has fixed size[11].
When I type characters more than 10, the rest of the characters are sent to the next scanf.
So my question is,
Is there any way to limit the size of input can be typed?
Or, is there any way to cut off the rest of the characters when the input exceeds the size of a char array, not sending to the next scanf?
Or, should I just know how many characters to type?
Or, should I create conditions to prevent this issue?
int main(void)
{
char phoneNum[11];
char password[11];
printf("Enter the phone Number: ");
scanf("%10s", phoneNum);
printf("Enter the password: ");
scanf(" %10s", password);
printf("\n\n");
//Display
printf("Phone number: %s\n", phoneNum);
printf("Password : %s\n", password);
return 0;
}
/*
What I tried:
-input:
Enter the phone Number: 123-456-7890
Enter the password: // This part has been skipped.
-output:
Phone number: 123-456-78
Password: 90
What I expected:
-input:
Enter the phone Number: 123-456-7890
Enter the password: asdf12386945648
-output:
Phone number: 123-456-78
Password: asdf123869
*/