dimanche 26 mars 2017

how to compare string in c program

I have a question about how to compare strings in 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 the error i get is

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 i don't know how to do it.

Aucun commentaire:

Enregistrer un commentaire