You’re getting this warning because there are code paths where key
is not set and subsequently used.
The isupper
and islower
functions are not opposites, e.g. a false return value from one doesn’t imply a true return value from the other. For example, c
contains the character '0'
, both isupper
and islower
will return false. When that happens, the value of key
is never set and so its value is indeterminate. This means that its value can’t be reliably predicted, and attempting to read it can in fact invoke undefined behavior.
You need to handle the case where both functions return false:
int shift(char c){
int key;
if (isupper(c)){
key = c - 65;
}else if (islower(c)){
key = c - 97;
} else {
key = c;
}
return key;
}
Or alternaltely:
int shift(char c){
if (isupper(c)){
return c - 65;
}else if (islower(c)){
return c - 97;
} else {
return c;
}
}
1
solved What Does “Error: Variable is used unitialized Whenever “if” Condition is False” mean? [closed]