Because name and vname do not contain strings. By specifying a size of 4 for each of them, with a 4-character string as initializer, you told the compiler to save only those 4 characters without the '\0' null character that marks the end of the string.
Undefined behavior; you are (un) lucky this is not just a glitch.
Delete 4 (or change it to 5 ):
char name[] = "sara"; char vname[] = "sara";
EDIT . Here is a modified version of your program that fixes a number of other problems (see comments). Other that omits 4 in the name and vname , most of the changes are not directly related to your question.
#include <stdio.h> /* needed for printf */ #include <string.h> /* needed for strcmp */ int main(void) /* correct declaration of "main" */ { char name[] = "sara"; /* omit 4 */ char vname[] = "sara"; /* omit 4 */ if (strcmp(name, vname) == 0) { printf("OK\n"); /* \n is at the *end* of the line */ } else { printf("Error\n"); /* as above */ } return 0; /* not absolutely required, but good style */ }
source share