Madhav Bajaj
Madhav Bajaj

Reputation: 21

Strcmp function and char arrays

The strcmp function in c is showing some weird behavior.

#include<stdio.h>
#include<string.h>
int main()
{
   char inst1[] = {'A'};
   char inst2[] = {'A'};    
   printf("%d\n",strcmp(inst1, inst2));

   return 0;
}

The above code produces an output 65, but it should have produced 0 from my understanding. Even more weird behavior follows when I run a slightly different code.

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

int main()
{
   char inst1[] = {'A'};
   char inst2[] = {'A'};
    
   char inst3[] = {'B'};
   char inst4[] = {'B'};

   printf("%d\n",strcmp(inst1, inst2));
   printf("%d\n",strcmp(inst3, inst4));

   return 0;
}

The above code produces an output-

-1
66

I have no idea why strcmp is producing such outputs.

Upvotes: 0

Views: 546

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

The function strcmp is designed to compare strings that is sequences of characters terminated by the zero-terminating character '\0'.

However these declarations

char inst1[] = {'A'};
char inst2[] = {'A'};

do not initialize the character arrays with strings.

So using the function strcmp with character arrays that do not store strings invokes undefined behavior.

You should write at least

char inst1[] = { "A" };
char inst2[] = { "A" };

that is to initialize the arrays with string literals.

Pay attention to that for example the string literal "A" is internally stored as a character array with two elements { 'A', '\0' } and the both elements are used to initialize character arrays that have enough space.

Thus for these declarations

char inst1[] = {'A'};
char inst2[] = {'A'};

sizeof( inst1 ) and sizeof( inst2 ) are equal to 1 while for these declarations

char inst1[] = { "A" };
char inst2[] = { "A" };

sizeof( inst1 ) and sizeof( inst2 ) are equal to 2

For these declarations

char inst1[] = {'A'};
char inst2[] = {'A'};

you should use the function memcmp instead of strcmp that is designed to compare sequences of bytes like

printf( "%d\n", memcmp(inst1, inst2, sizeof( inst1 ) );

You could also use the function strncmp but for arrays that do not contain strings using the function is less efficient than using the function memcmp.

Here is a demonstrative program.

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

int main(void) 
{
    {
        char inst1[] = { 'A' };
        char inst2[] = { 'A' };    
        
        printf( "%d\n", memcmp( inst1, inst2, sizeof( inst1 ) ) );
    }

    {
        char inst1[] = { "A" };
        char inst2[] = { "A" };    
        
        printf( "%d\n", strcmp( inst1, inst2 ) );
    }
    
    return 0;
}

The program output is

0
0

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

Your arrays are not null-terminated, so passing them to strcmp() is not allowed because strcmp() expects strings (null-terminated sequence of characters) and passing non-null-terminated arrays will lead to out-of-range access looking for terminating null-character.

You should add terminating null-character like this:

#include<stdio.h>
#include<string.h>
int main()
{
   char inst1[] = {'A', '\0'};
   char inst2[] = {'A', '\0'};    
   printf("%d\n",strcmp(inst1, inst2));

   return 0;
}

Or easier version:

#include<stdio.h>
#include<string.h>
int main()
{
   char inst1[] = "A";
   char inst2[] = "A";    
   printf("%d\n",strcmp(inst1, inst2));

   return 0;
}

Upvotes: 4

Related Questions