+ 2
How to perform equality check on chars
I'm trying to perform an equality check on chars based on user input but I always get an Error when I try this. This is just a sample code of what I'm talking about char opp[1]; scanf("%c",opp); if(opp == 'c') { printf("hello"); } But I always get an Error on that if statement Thanks in advance
4 Réponses
+ 3
no need char array..
Just put
char opp;
+ 3
Thanks but I'm hit with segmentation fault
+ 2
Also You need &opp for taking char... Chidera
char opp;
scanf("%c",&opp);
if(opp == 'c')
{
printf("hello");
}
+ 2
I suggest you use a single char, not char[], but I assume you are asking "What if I use char[1]?", the reason why you get an error is that when you make it char[], opp is the same as '&opp[0]', so your scanf is fine since opp can be said as &opp[0]. However, your if is wrong, you are trying to compare a constant char with an address of opp[0], instead, try 'if (*opp == 'c')', that way, you are comparing a pointer that points to a character with a character.