Morning,
with this program, I prompt the user to enter a character and if it is a lower case letter, it would return its uppercase value.
Here is my code:
Code:
int toUpper(int x);
main()
{
char c;
printf("Enter character: ");
scanf("%c", &c);
printf("Uppercase = %c\n", toUpper(c));
}
int toUpper(int x)
{
char upper;
if(x >= 'a' && x <= 'z')
{
upper = x - 32;
return upper;
}
}
It definitely works for lowercase letters. But for some reason, conditions that doesn't fit the boolean expression
Code:
if(x >= 'a' && x <= 'z');
still would execute.
For instance here are two sampled runs:
Code:
Enter character: b
Uppercase = B
Code:
Enter character: B
Uppercase = "
B in decimal is 66 and " in decimal is 34 which 66 - 34 = 32, which shows that it executes. Is there an error in my boolean expression that allows this to happen? I even tried changing the conditions to decimal values but still produces the same outcome.