Below you will find the code that I wrote. So far, this code is to check if the user entered a small letter or a larger letter by scanning the input from the user which is a character and passing it to a function to return either 1 or 0 depending this is letter is in caps or not. So, what I don't understand is why "please enter a letter 0" is printed. My best guess is because scanf
stacked the null character and it is saved so it had to check if this letter is in caps or not as well. But if I am right, can you please tell me a way to avoid this problem from happening??? Thanks a lot in advance.
#include <stdio.h>
#include <stdlib.h>
char CapsCheck (char c)
{
if (c >= 65 && c<= 90 )
return 1;
else
return 0;
}
int main()
{
char c;
while (1)
{
printf("please enter a letter: ");
scanf("%c",&c);
printf("%d\n",CapsCheck(c));
}
return 0;
}
Output:
please enter a letter: C
1
please enter a letter: 0
please enter a letter:
I explained everything in the details section but you can ask me in the replies if you wanted any more inquiries. PS: I am using Code blocks.