The following program does not work as I intended.
#include <string.h>
#include <stdio.h>
int main()
{
const char *c = "abcdef";
// prints 'f' as expected
printf("%c\n", c[5]);
// comparison fails, does not print "yes"
if (c[5] == 'f')
printf("yes");
return 0;
}
How can I compare a character in a string literal to a character value? Is it possible without using ASCII related functions i.e. chr() or ord() suppose those exist. ( I remember using them back in Pascal days)
Thanks
There must be something else going on with your environment. I just threw that into gcc and it prints out “yes” as expected.
Are you sure you aren’t just missing the “yes” because printf does not add a new line by default, so it could be “yes[your shell prompt]” and you’re just overlooking the yes?