tags:

views:

182

answers:

6

Suppose I have a character array d[20] which has d[0]='i' and d[1]='f' and I assign null value d[2]='\0': What is value of strcmp(d,"if") and why?

I expect strcmp to return a value of 0. when i run it i get a value of -1 and not 0 ..

+1  A: 

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

Niall C.
+6  A: 

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
+1  A: 

The null value is indicated by:

d[2]='\0'

and not /0 as you wrote.

Mikeage
+1  A: 

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
+2  A: 

@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
+1  A: 

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;
}
drewk