how to check a string in if

Asked

Viewed 560 times

-1

I have the code: Why it is not giving positive for the if?

{
    char placa[9] = "G" "A" "E" "-" "0" "2" "4" "4";
    char cor[15] = "branco";
    int ano = 2001;

    printf("A placa: %s\n", placa);
    printf("A cor: %s\n", cor);
    printf("O ano: %d\n", ano);

    if(cor == "branco")
    {
        printf("voce tem um carro branco \n");
    }
    else 
    { 
        printf("voce nao tem um carro branco \n");
    }

    system("PAUSE");

    return 0;
}

1 answer

0

color is a pointer to an array of chars then when you make the comparison

cor == "branco" 

you are comparing the value of pointers and not the words, to compare the words you can use the function strcmp (there are several ways to make this comparison, such as creating your own function or using one from a library you are using).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.