0

when I explicitly state the value of a string, then compare it to itself, system returns FALSE. Does this have something to do with the extra '\0' character added by the system? And how should I refine my code to make it TRUE?

char name[5] = "hello";

if(name == "hello")
{
    ...
}
reiallenramos
  • 1,235
  • 2
  • 21
  • 29
  • 6
    In C you can't compare strings like that, what you are doing is comparing two *pointers* that will never be equal. Read about [`strcmp`](http://en.cppreference.com/w/c/string/byte/strcmp) for how to compare strings. Also, remember that strings needs to be *terminated*. A string of five characters needs space for *six* characters to include the terminator. Both of these facts should be in any [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Apr 22 '17 at 08:04
  • 1
    You need `char name[6] = "hello";` to hold `'hello` (or even better `char name[] = "hello";`) if you intend to use `name` as a *string*. Why? (hint: you forgot `+1` for the *nul-terminating* character `:)` If you do not plan to use `name` as a *string`, then know you cannot use any of the `string.h` function that expect a *nul-terminated string* as a parameter. – David C. Rankin Apr 22 '17 at 08:13
  • @DavidC.Rankin I tried `char name1[] = "hello";` then `char name2[] = "hello";` and finally `strcmp(name1, name2)` but still FALSE. How can something so simple be so frustrating... – reiallenramos Apr 22 '17 at 08:20
  • Related: http://stackoverflow.com/q/11399682/694576 – alk Apr 22 '17 at 08:22
  • 1
    `(strcmp(name1, name2) == 0)` works. I looked at documentation. Dumb me. – reiallenramos Apr 22 '17 at 08:23
  • @reiallenramos, that isn't frustrating, that is correct. If the strings are equal `if (strcmp(name1, name2) == 0)` then the strings are equal `:)` – David C. Rankin Apr 22 '17 at 08:24
  • Change `char name[5]` to `char name[]` - Get the compiler to compute the right size for you (PS: It is not 5) – Ed Heal Apr 22 '17 at 08:25
  • @EdHeal `char name[] = "hello";` works but when change it to `char name[];` only, it gives an error. Why is this? – reiallenramos Apr 22 '17 at 08:26
  • `char name[] = "hello";` - Compiler is told to create an array of characters? What is it contents/size - It can find that out from `"Hello"`. `char name[]`. How can the compiler find out the size. It does not know. – Ed Heal Apr 22 '17 at 08:35
  • As it stands `name` isn't a C-string, as it is missing the `0`-terminator, as it is defined to be *only* five `char`s long. As it isn't a C-"string", passing it to any member of the `str*()` family of functions invokes undefined behaviour. – alk Apr 22 '17 at 08:47

5 Answers5

5

You can't (usefully) compare strings using != or ==, you need to use strcmp The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings. don't use predefined array size like char name[5] = "hello"; instead of you can use char name[] = "hello"; or char name[6] = "hello"; when use

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

int main()
{
char a[] = "hello"; 
char b[] = "hello";

   if (strcmp(a,b) == 0)
      printf("strings are equal.\n");
   else
      printf("strings are not equal.\n");

   return 0;
}
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
3

Continuing from my comment, you need char name[6] = "hello"; to hold 'hello (plus the nul-terminating character) Even better, you can use

char name[] = "hello";

That will properly initialize name to contain 6-character (including the nul-byte).

All string.h functions expect a nul-terminated string as a parameter when they take char * or const char * as parameters passed to the functions.

Lastly, as correctly noted in Anuvansh's answer, you cannot use a inequality condition to determine if two strings equal or differ. You either us the normal comparison functions strcmp, strncmp, memcmp or you walk a pointer down each string stopping at the first char the strings differ on, or on the nul-byte if the strings are equivalent.

Look things over and let me know if you have any further questions. Gook luck with your coding.

alk
  • 69,737
  • 10
  • 105
  • 255
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

The strcmp() returns 0 if both argument are equal.

char name[]="hello";
if(strcmp(name,"hello") == 0)
    return TRUE;
else
    return FALSE;
BEPP
  • 875
  • 1
  • 12
  • 36
0

Actually, name is a pointer that points to the address of the string "hello". You can't compare them. So you can try strcmp function instead. Also include the string.h library.

like:

strcmp(name,"hello");

also as one of the comment points out, take a char array of 6 to include '\0'.

Hope that helps.

m7913d
  • 10,244
  • 7
  • 28
  • 56
0

In C ,the array name is actually the pointer to the first element of that array.

in your case:

if(name == "hello")

you compare pointer to the string so it will return false

You can see the same concept in this article why is array name a pointer to the first element of the array?

You can simply include "string.h" library and use strcmp() function

code like this:

char name[]="hello";

if(strcmp(name,"hello")==0){

.....

}

to make it Ture

Community
  • 1
  • 1
楊承諺
  • 1
  • 1
  • Indent your code by 4-characters to format it as code. – David C. Rankin Apr 22 '17 at 08:25
  • "*You can simply include library*" In C libraries are not used during compilation, but are *linked after* compilation. What is *included* for compilation is/are the *prototype/s* of the function/s provided by the libraries . – alk Apr 22 '17 at 08:55