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;

22nd May 2021, 9:47 AM
Sandesh Adhikari
Sandesh Adhikari - avatar
2 odpowiedzi
+ 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
22nd May 2021, 9:56 AM
Shadow
Shadow - avatar
+ 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.
22nd May 2021, 10:00 AM
sarada lakshmi
sarada lakshmi - avatar