0
How do I compare a character with ASCII value in C?
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { char ch='A'; int no=97; if(no==ch) //I know there is other way of doing this but I have no idea { printf("This is alphabet %c",ch); //It is will not be executed } return 0;
2 Respostas
+ 6
The ASCII value of the character 'A' is 65, not 97 ('a'), which is why your comparison fails. You can review a ASCII table, e.g. here:
https://www.rapidtables.com/code/text/ascii-table.html
+ 1
#include <stdio.h>
int main()
{
char ch='a';
int no=97;
int a = (int)ch;
if(a==no)
{
printf("This is alphabet %c",ch);
}
return 0;
}
Use type casting concept.