string - Why will this simple C program not run? -
this code not run. can tell me why?
#include <stdio.h> #include <string.h> main (){ char user[7]; printf("username\n"); scanf("%s",user); if(user == 'admin'){ printf("hello world"); }else{ printf("bad"); } return(0); }
this working example. need use strcmp compare strings. strcmp() returns 0, if strings equal.
if max input length known, should use length specifier in scanf or 1 of suggestion listed here, prevent buffer overflow.
#include <stdio.h> #include <string.h> int main(int argc, char* argv) { char user[7]; printf("username:\n"); scanf("%6s", user); if(!strcmp(user, "admin")) { printf("hello world"); } else { printf("bad"); } return 0; }
Comments
Post a Comment