I’m trying to compare a regulare text to my input and if this specific condition is happening i want to print something, if not, just keep running.
This is the code:
int main()
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
if (c == 'nir')
{
printf ("you typed nir");
}
c = getchar();
}
}
The question is a little too confused for me to answer it all at once, so let’s begin at the beginning. To begin,
'nir'(if it compiles at all) is a very strange way to write a single integer, whereas I think that you meant"nir", a string of text. However,(c == "nir")does not compile, does it? [This presumably is why you have tried(c == 'nir')as a random shot, isn’t it, though it means nothing sensible in C.]In the context in which you use it, the
"nir"is an address. Now, you probably don’t know what this means yet — nor will you until you have gained several weeks’ more experience in C. What you can use today is the standard library’sstrcmp()function, presented by string.h. I would recommend that you look that up and go from there.And don’t type
'nir'again, incidentally! It means nothing sensible in C. The single quotes are for a single character like'n', and stand for the character’s integral ASCII value.