I have a question about how to compare strings in an if
statement. I am moving from Python to C and the comparing strings is easy in Python, but in C how do I do it?
My program is:
printf("Enter your choice 1.add\n 2.sub\n 3.mul\n 4.div\n");
string choice = get_string();
if (choice == "add")
{
int c = calculate_add(a, b);
printf("sum of %i and %i is %i\n", a, b, c);
}
When I run this, I get this error:
calculate.c:19:16: error: result of comparison against a string literal is
unspecified (use strncmp instead) [-Werror,-Wstring-compare]
if (choice == "add")
^ ~~~~~
It says use strncmp
to compare the string, but how do I do it?