I have created a substring from a string, but the equality condition doesn't seem to hold when checked with the exact same string.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
char per[] = "wefgq retc";
int c = 0;
char sub[10];
while (c < 4) {
sub[c] = per[c];
c++;
}
sub[c] = '\0';
char ler[] = "wefg";
printf("%s,\n", sub);
if (ler == sub) {printf("same");}
else {printf("not same");}
}
The output of this code when compiled and executed is-
wefg,
not same
can somebody please explain why this is happening? Is there something wrong with the way I have created the string, like the '\0' character? Edit- I used strcmp() instead of == but still got the same result.