mardi 16 novembre 2021

Why if else statements are not working for an array?

#include <stdio.h>

int main() {
    char array[2][2] = {"-", "+"};
    printf("%s", array[0]);
    
    if(array[0] == "-"){
        printf("Its a minus/n");
    }
    else{
        printf("\nNothing");
    }
    
    return 0;
}

Every time I run the code, only else gets executed. I have explicitly mentioned that if 0th index of array is equal to a string minus then print its a minus.

I have declared char array[2][2] character array. Stored only one element in it regardless of limit being of 2 elements. And I am calling array[0] because I want only first character.


if(array[0] == "-"){
    printf("Its a minus/n");
}
else{
    printf("\nNothing");
}

Does anyone has a solution for it?

Aucun commentaire:

Enregistrer un commentaire