1

I am learning the C programming language, I am attempting to do an If statement in which if the user inputs "hey" in reads the var is hey, if not it reads, the var is not hey, however, when executing, even when hey is inputed, it reads, the var is not hey, how can I fix this?

#include <stdio.h>
void main(void){
        char var[3];
        printf("Enter your name: ");
        scanf("%s", &var);
        printf("%s", &var);
        printf("\n");
        if(var == "hey"){
                printf("The var is hey");
        }
        if(var != "hey"){
                printf("The var is not hey");
        }
        printf("\n");
        }
gsamaras
  • 71,951
  • 46
  • 188
  • 305
J. Doe
  • 53
  • 3
  • 7

1 Answers1

0

Use strcmp() to compare strings n C, like this:

if(strcmp(var, "hey") == 0) {
    printf("The var is hey");
}
else {
    printf("The var is not hey");
}

PS: We usually write int main(void) {}. Read more in What should main() return in C and C++?

gsamaras
  • 71,951
  • 46
  • 188
  • 305