I want to test and see if a variable of type “char” can compare with a regular string like “cheese” for a comparison like:
#include <stdio.h>
int main()
{
char favoriteDairyProduct[30];
scanf("%s",favoriteDairyProduct);
if(favoriteDairyProduct == "cheese")
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}
return 0;
}
(What I actually want to do is much longer than this but this is the main part I’m stuck on.)
So how would one compare two strings in C?
You’re looking for the function
strcmp, orstrncmpfromstring.h.Since strings are just arrays, you need to compare each character, so this function will do that for you:
Further reading: strcmp at cplusplus.com