C cannot figure out if statement -
this question has answer here:
- how compare strings? 6 answers
so trying learn c can't figure out why code won't run properly.
#include <stdio.h> #include <stdlib.h> int main() { char username[25]; char myname[25] = "myname"; printf("please enter name: \n"); scanf("%s", username); if(username == myname) { printf("congratulations name myname!!!"); } else { printf("your name %s how disappointing...", username); } return 0; }
the problem if statement never seems return true. can me this?
this line comparing locations of strings, different, since comparing 2 different strings.
if(username == myname)
the correct test in c use library function.
#include <string.h> ... if(strcmp(username,myname) == 0)
Comments
Post a Comment