-4

In C, I have a character array:

d[20] 

Which is assigned the value 'if' with a null termination character:

d[0]='i'
d[1]='f'
d[2]='\0'

Should the value of strcmp(d,"if") be 0? Why?

I expect strcmp to return a value of 0. When I run it, I get the value of -1

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Hick
  • 35,524
  • 46
  • 151
  • 243
  • d[2] = '/0'???? Shouldn't that be d[2] = '\0'; – Torlack Apr 19 '10 at 17:02
  • 10
    Downvoted for assuming error in standard library function. ALWAYS assume that your code has problems, not library. And post the code. – qrdl Apr 19 '10 at 17:04
  • 4
    @mekasperasky: Here's what you do when you think you've found an error in a compiler or standard library: 1) You boil the problem down to a 20 lines of code compilable program that reproduces the problem. 2) You post that example in a from that can be pasted into an editor, compiled, and tested. 3) You accompany the program by the compiler/std lib version you used to reproduce the problem. - IME step #1 will in 98% of all cases reveal _your_ error. In 1.98% critique caused by step #2 will find _your_ error, too. report the remaining 0.2% to the vendor, using your reprodrucing program as proof. – sbi Apr 19 '10 at 17:12
  • 1
    @sbi, you think it's as high as 0.2%? Back when I used to hang out in comp.lang.c, I'd say it was more like 0.0001%. – Paul Tomblin Apr 19 '10 at 17:29
  • @Paul Tomblin: Depends on the language and such. If this is a mainstream implementation C library, I'd estimate the chances that this is a problem with `strcmp()` to be comparable to my winning the lottery and retiring in luxury. I've worked with languages where that didn't apply. – David Thornley Apr 19 '10 at 17:37
  • While the title of the question might lead one to believe that mekasperasky thinks `strcmp()` is broken, I think that the question is really asking "why isn't `strcmp()` behaving as I expect?" or "problem using strcmp()". I think this is a legitimate beginner question, especially given that the problem appears to be partly the result of character literals that 'contain' more than one character - which I think may be non-intuitive to many programmers (particularly beginners). – Michael Burr Apr 19 '10 at 17:46
  • 2
    @Michael Burr: mekasperasky said in a comment that the '/0' was a typo, which leaves me not knowing what's going on. This question cannot be answered without a real code sample and run report, which I'm not seeing show up. – David Thornley Apr 19 '10 at 17:49
  • 1
    @David Thornley: I see - it is a confusing situation. We'll see if mekasperasky clarifies (hopefully with a copy-n-paste from the actual code). – Michael Burr Apr 19 '10 at 18:11
  • @Paul: Ok, I'll admit: That 0.2% are my current estimates for _me_, after >15 years of banging my head against such problems in C++. It would be very different for a newbie in C. Mea culpa. – sbi Apr 19 '10 at 19:06

6 Answers6

6

If you mean d[0] = 'i'; d[1] = 'f'; d[2] = '\0';, then yes, that should return 0. d[2] = '/0' will assign something entirely different and your string won't be null terminated. At least not where you expect it to be - strcmp will probably head off into the weeds and start sucking mud.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
2

@everyone: The '/0' typo was introduced by @Mark when he edited the original question. The original had the proper '\0'. Most of your answers (and assumptions about the OP) are misdirected.

@mekasperasky The following code correctly produces the value of 0. If you can compare it to your personal code and find the difference, you may have solved your own problem.

int main(int argc, char* argv[])
{
   char d[20] = {0};
   d[0] = 'i';
   d[1] = 'f';
   d[2] = '\0';

   int value = strcmp(d,"if");
   cout << value << endl;
   return 0;
}
KevenK
  • 2,975
  • 3
  • 26
  • 33
1

d[2] should be '\0', not '/0'.

Niall C.
  • 10,878
  • 7
  • 69
  • 61
1

The null value is indicated by:

d[2]='\0'

and not /0 as you wrote.

Mikeage
  • 6,424
  • 4
  • 36
  • 54
1

As other answers have mentioned, '/0' is not a null termination character, '\0' is.

You might expect that specifying more than one character in a character literal might generate an error; unfortunately (at least in this case) C allows 'multi-character' literal characters - but the exact behavior of multi-character literals is implementation defined (6.4.4.4/2 "Character constants"):

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

So your '/0' 'character' ends up being some implementation defined int value that gets truncated when stored in d[2]. Your compiler might generate a warning for 'multi-character' literals, but that would probably also depend on the exact options you give the compiler.

For example, I get the following warning from GCC (I happen to have -Wall set):

C:\temp\test.cpp:6:14: warning: multi-character character constant

In my tests with MSVC and MinGW, the value of '/0' is 0x00002f30, so d[2] = '/0' ends up being equivalent to d[2] = '0'.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
1

This works on every C compiler I have. I did not expect differently. It also works on Codepad.

Does this work on your C compiler?

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

char d[20];

int main(void) {
   d[0]='i';
   d[1]='f';
   d[2]='\0';

   printf("Value of strcmp=%i\n\n",strcmp(d,"if"));  /* will print 0 */

   return 0;
}
dawg
  • 98,345
  • 23
  • 131
  • 206