0
int main(){

  char password[10];
  int i = 0;
  printf("Please enter your password\n");
  scanf("%s", password);

  while (password != "juniorkid"){

  printf("Error\n");

  printf("Please enter your password\n");
  scanf("%s", password);

  i++;

  if (i>1){
    printf("Exceeded tries");
    break;

  }
else{
  printf("yay\n");
}

  }


}

 clang-7 -pthread -lm -o main main.c main.c:20:19: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] while (password != "juniorkid"){ ^ ~~~~~~~~~~ 1 warning generated.  ./main

  • You can not compare strings with a mathematical operator in C. It would end up comparing the addresses, since strings are just a pointer to an address. You need to use strcmp function – Irelia Jun 03 '20 at 18:43
  • 1
    Does this answer your question? [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – Irelia Jun 03 '20 at 18:45
  • @Nina *`since strings are just a pointer to an address`* No. They are not. C strings are **char arrays** but not pointers. – 0___________ Jun 03 '20 at 19:01
  • Are you impling char arrays are not Pointers?..... And does comparing c strings with a mathematical opator not compare their address? Nitpicking at every small thing isn't benefiting anyone.. Besides you basically repeated the same thing Isaid but as an answer instead of a comment even though the question had already been answered before which I posted a link to...BIG YIKES my dude – Irelia Jun 03 '20 at 19:11

1 Answers1

1

password != "juniorkid" it does not compare the char arrays (C strings). It only compares the addresses of the char arrays and (which is obvious as they do not occupy the same memory)

you need to use string comparison function strcmp

0___________
  • 60,014
  • 4
  • 34
  • 74