1

I am comparing two strings a and b in the code below. strcmp() returns 0 saying that the strings are equal. But the comparision operator == returns false. Is it due to the difference in memory allocated to these strings? If so, why doesn't a comparision like (10 == 10.0) yeild false?

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

int main(void)
{
    char a[] = "hello";
    char b[10];
    strcpy(b,a);
    printf("%s %s\n",a,b);
    printf("%d\n", strcmp(a,b));
    printf("%d\n", (a==b));
    
}

output

hello hello
0
0
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Diwas10
  • 105
  • 7
  • In most contexts arrays are converted ("decay") to a pointer to their first element. Comparison context is not an exception, so `a == b` compares two pointers. You might like section 6 of the [comp.lang.c faq](http://c-faq.com/). – pmg Mar 12 '22 at 10:25
  • In addition to what pmg mentioned, these two pointers are the addresses of distinct variables that exist at the same time. They can never be identical. – Gerhardh Mar 12 '22 at 10:30
  • 2
    Also you may want to `printf("%p %p\n", (void*)a, (void*)b);` – pmg Mar 12 '22 at 11:01

1 Answers1

2

In this expression with the equality operator

(a==b)

the both operands having array types are implicitly converted to pointers to their first elements. In fact the expression above may be rewritten like

( &a[0] == &b[0] )

As the arrays occupy different extents of memory then this comparison will always yield 0.

Pay attention to that if you will write for example

( "hello" == "hello" )

that may be also rewritten like

( &"hello"[0] == &"hello"[0] )

then the result of the expression is unspecified. That is the compiler can store identical string literals as separate character arrays or as one character array depending on compiler options.

For example in MS VS C++ there are compiler options /GF and /GF- that allow either to create one character array for identical string literals or different arrays. This option is well-documented and can be set in properties of a given project.

As for this expression

(10 == 10.0)

then to perform the operation the compiler needs to determine the common type of the operands using the usual arithmetic conversions. That is the operand with the type int (10) is converted to the type double and two double values are compared.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • The result of `"hello" == "hello"` is unspecified, not implementation-defined. “Implementation-defined” means the C standard requires each implementation to document the choice. The C standard does not require implementations to define whether identical string literals are distinct. C 2018 6.4.5 7 says “It is unspecified whether these arrays are distinct provided their elements have the appropriate values…” – Eric Postpischil Mar 12 '22 at 10:50
  • @EricPostpischil The choice is documented in the description of the compiler options. – Vlad from Moscow Mar 12 '22 at 10:57
  • “Implementation-defined” means the C standard requires each implementation to document the choice. The C standard does not make such a requirement for this behavior. So it is not implementation-defined. Even if a particular C implementation chooses to document what it does, that does not make the behavior “implementation-defined” in the meaning of the C standard. – Eric Postpischil Mar 12 '22 at 11:47
  • @EricPostpischil It is implemenetation-defined because it depends on the compiler implementation and this behavior well-documented in each compiler. If the compiler says that the behavior is unspecified this does not prevent the behavior to be implementation defined. – Vlad from Moscow Mar 12 '22 at 11:52
  • C 2018 3.4.1 1: “**implementation-defined behavior** unspecified behavior where each implementation documents how the choice is made”. That is what “implementation-defined” means. It does not mean some implementation chooses to document the choice. It means the C standard asserts that each implementation documents the choice. It is a requirement upon the implementation. For string literals, the C standard does **not** require that implementations document the choice. So it is not “implementation-defined” in the meaning of the C standard. Stop misusing defined terms. – Eric Postpischil Mar 12 '22 at 12:17