I was checking library function isupper() to check if the function version or macro version of
isupper() is efficient depending on storage used or runtime and I have this peculiar problem
when using scanf() for storing input character to a int varibale.
Here is my code-
#include<stdio.h>
int my_isupper(int );
int main() {
int c;
printf("Enter a alphabet to check if it is upper case or notn");
//c = getchar(); // getchar(), getc() works fine though
scanf("%c", &c);
/*
Doesn't work when c is a int but not initialized to 0
Works fine on some machine no matter c is a char, int or not initialized to 0
Is it compiler/machine dependent? if so, which part ( scanf()?) has dependency?
*/
printf("test1: %dn", c);
if(my_isupper(c))
printf("%c is upper casen", c);
else
printf("%c is not upper casen", c);
return 0;
}
int my_isupper(int c) {
printf("test2: %cn", c);
int value = (c >= 'A' && c <= 'Z')? 1 : 0;
printf("test3: %dn", value);
return value;
}
when the variable c is set as char it works fine. when it is set as int the program works fine
with getchar(), getc() etc library functions but when using scanf(), if the variable is set as
int and not initialized to 0 then scanf() is storing 32577 for the char ‘A’ , 32578 for the
char ‘B’ and so on.
When giving input: A , return value should be 1, but the return value I am getting is 0 as the
condition is not satisfied because scanf() is saving 32577 for the char A, and 32778 for char B
and so on.
Unix C Kernel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.