0

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.

  • 4
    To compare strings in C you need to use a dedicated function like `strcmp` – Eugene Sh. Mar 03 '21 at 17:03
  • 1
    To elaborate on @EugeneSh. 's comment, `if (ler == sub)` is not comparing the contents of the arrays, but rather the addresses of the first elements of each array due to [pointer decay](https://stackoverflow.com/q/1461432/3282436). – 0x5453 Mar 03 '21 at 17:05

1 Answers1

1

It can not be compared directly like if(str1==str2), as this will only compare the decayed pointers of the character arrays with each other. It will not compare the actual string contents. You need to use the strcmp() function to compare the actual string contents.

Example:

#include <stdio.h>
#include <string.h>

    int main()
    {
        char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
        int result;
    
        // comparing strings str1 and str2
        result = strcmp(str1, str2);
        printf("strcmp(str1, str2) = %d\n", result);
    
        // comparing strings str1 and str3
        result = strcmp(str1, str3);
        printf("strcmp(str1, str3) = %d\n", result);
    
        return 0;
    }
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
iamdhavalparmar
  • 1,090
  • 6
  • 23
  • Of course there are strings in C. – Crowman Mar 03 '21 at 17:07
  • now is it fine ? @Crowman – iamdhavalparmar Mar 03 '21 at 17:15
  • 1
    @Crowman, just to add some pedantry: There are no string *datatype* in standard c, only a string *convention*. – HAL9000 Mar 03 '21 at 17:25
  • 2
    In [§7.1.1 ¶1 of the ISO C11 standard](http://port70.net/~nsz/c/c11/n1570.html#7.1.1p1), a string is defined in the following way: `"A string is a contiguous sequence of characters terminated by and including the first null character."` – Andreas Wenzel Mar 03 '21 at 17:27
  • 1
    Thanks, resloved mow! – Prithwish Maiti Mar 03 '21 at 17:33
  • @HAL9000: What Andreas said - a string is not a "convention" in C, it's a concrete thing with a formal definition. It's not a type, certainly, but that's a long way from saying it doesn't exist. – Crowman Mar 03 '21 at 18:12
  • @Crowman, yes, it is wrong to say that strings doesn't exist in c. And while the claim that there are strings in c is absolute correct, some clarification may be helpful to programmers that are more used to other languages. – HAL9000 Mar 03 '21 at 18:29