1

I'm having trouble comparing two strings in C to see if they are equal.

switch(i) {
case 1:
  printf("Got in case 1.  TextA=%s    word=%s \n",TextA,word);
  if(TextA == word) {       
    SubTypeOption = 1;
    printf("SubTypeOptioon = %d",SubTypeOption);
  }

On my output, i get "Got in case 1. TextA=SupTypeA word=SupTypeA SubTypeOption = 0" //So it wasn't changed somehow

user2055216
  • 61
  • 1
  • 6

6 Answers6

6

The == operator compares pointers. It succeeds only when the two char* point to the same address in memory. Comparing strings' content in C requires a call of strcmp or strncmp:

if (strcmp(TextA, word) == 0) {
    // Strings are identical
}

Note the comparison to zero: it is required because strcmp functions compare strings lexicographically, returning zero when the strings are equal. You need to include <strings.h> in order to use strcmp.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

use strcmp

not the '==' operator

http://www.cplusplus.com/reference/cstring/strcmp/

Forhad Ahmed
  • 1,761
  • 13
  • 18
2

You can't compare strings using the == symbol, because you are comparing whether they are placed in the same memory location or not. TextA and word are pointers to the first character of each c-string.

Used strcmp() or strlcmp() functions to compare strings. They'll return 0 if the two strings are equal

#define STREQ(a,b) (strcmp((a),(b))==0)
if (STREQ(TextA, word)){

}
Tom
  • 43,810
  • 29
  • 138
  • 169
1

Simple - use strcmp - that is the function that you require.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

You cannot compare the contents of strings (or any other array type) with the standard relational operators like ==, <, >, etc. You will need to use the strcmp library function instead:

#include <string.h>
...
if (strcmp(TextA, word) == 0)
{
  // strings are equal
}

strcmp will return an integer value < 0 if TextA is lexicographically less than word, 0 if they are lexicographically equal, and > 0 if TextA is lexicographically greater than word.

Note that, in the C locale, this means strings will be ordered "ASCIIbetically"; that is, any string beginning with 'a' will come after a string beginning with 'Z', since the ASCII code for 'a' is greater than 'Z'.

So why can't you use == for comparing strings?

Except when it is the operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T" will be converted to an expression of type "pointer to T", and its value will be the address of the first element of the array.

This means that, in the condition TextA == word, both of the expressions TextA and word are being converted to pointer values; instead of comparing the contents of the two arrays, we're comparing their addresses.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0
switch(i) {
case 1:
  printf("Got in case 1.  TextA=%s    word=%s \n",TextA,word);
  if(strcmp(TextA,word)==0) {       //something like that
    SubTypeOption = 1;
    printf("SubTypeOptioon = %d",SubTypeOption);
  }

http://www.cplusplus.com/reference/cstring/strcmp/

Arpit
  • 12,767
  • 3
  • 27
  • 40