Question

I wrote the following code in C.

#include <stdio.h>
#include <string.h>
int main(void) {
    char str1[4] = "abcd";
    char str2[4] = "abcd";
    printf("%d\n",strcmp(str1,str2));

    return 0;
}

I expected the return value to be 0 (as I am taught that strcmp function returns 0 for equal strings). But it prints 1!

Success  time: 0 memory: 2248 signal:0
1

Is it a bug? Or am I missing out on something?

Was it helpful?

Solution

Because your arrays are not long enough. Your are not taking into account the zero terminator of your strings. You need 5 chars for your string, four for the string itself plus one for the zero terminator.

Write:

char str1[5] = "abcd";
char str2[5] = "abcd";

BTW I wonder why your compiler does not issue a warning or does it ?

OTHER TIPS

(Courtesy Pascal Cuoq)

The C99 standard §6.7.8.¶14 says

An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

Since strings are terminated by the null byte '\0', the actual number of characters in the string literal "abcd" is 5. The size of the arrays str1 and str2 is 4. Therefore, they cannot hold the null byte and are, in fact, not strings. The below statements are equivalent.

char str1[4] = "abcd";
char str1[4] = {"abcd"}
char str1[4] = {'a', 'b', 'c', 'd'};

Passing str1 and str2 to strcmp invokes undefined behaviour because they are not strings. strcmp will overrun the buffer pointed to by str1 and str2 since it cannot find the terminating null byte. This is undefined behaviour and may result in program crash due to segfault.

A string is a character array terminated by the null byte '\0'. Therefore, the length of the string literal "abcd" is 5 and not 4. Note that the standard library function strlen does not count the null byte so in this case, strlen("abcd") returns 4.

When you initialize an array with a string literal, it is a good practice to leave your array size blank which is automatically determined to be large enough to store all the characters in the string literal it is initialized with.

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

int main(void) {
    char str1[] = "abcd";
    char str2[] = "abcd";
    printf("%d\n", strcmp(str1, str2));  // prints 0

    return 0;
}

One of your strings looks like this in memory: 'a' 'b' 'c' 'd' '\0'. That is, like every string it is terminated by '\0' which is a character just like 'a' or 'b'. Therefore you need room for five chars in order to store the string "abcd" and must declare it by char str1[5].

Either you give correct size for your arrays, either you let the compiler to do everything for you

 char str1[] = "abcd";
 char str2[] = "abcd";

in this case, compiler will give enough space for your strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top