1
votes

I am trying to implement a string compare function where I want to return a bool value but I am getting several errors and I don't know why is that so.

The error that the compiler shows is

"error: unknown type name bool" 
&
"error: false, true undeclared" 

now as far as I know, bool has only two values i.e. true or false, then why am I still having problem?

bool string_cmp(char word1[],char word2[]){
    bool  isEqual = false;
    int i,k=sizeof(word1);
    for(i=0;i<=k;i++){
        if(word1[i]!=word2[i]){
            isEqual=false;
        }
        else
            isEqual=true;
            break;
    }
    return isEqual;
}

Edited code(why is my output wrong when I am adding more words in word1 array?):

bool string_cmp(char word1[],char word2[]){
    bool  isEqual = false;
    int i,k=sizeof(word1);
    for(i=0;i<=k;i++){
        if(word1[i]!=word2[i]){
            isEqual=false;
            break;
        }
        else
            isEqual=true;
    }
    return isEqual;
}

int main()
{
    int count;
    bool cmp;
    char word1[40]={"Hi world world"},word2[20]={"Hi world"},result[60]; //the only case when I am getting wrong output; otherwise if both words are same or if I remove something from word2, I get the output as expected.
    cmp=string_cmp(word1,word2);
    printf("%d",cmp);
}
2
bool is not a built-in type. Refer this: stackoverflow.com/questions/1921539/using-boolean-values-in-cuser218867
Your else condition has multiple statements. You need to add braces. You cannot reliably get the sizeof word1. You should get the length using strlen(). i<= would cause an off by one error. Change that to <. You should early terminate in the if condition.Rafael
k=sizeof(word1) should be k=strlen(word1); and also you have logic erors in your loopM.M

2 Answers

2
votes

you have to do:

#include <stdbool.h> 

in c programs in order to hjave access to bool type and the true/false definitions.

0
votes

In C++, bool, true and false are keywords and available everywhere. This is not the case in C.

C only got language support for boolean types with the C99 standard, which introduced the keyword _Bool. There are no true or false keywords, since C historically uses 1 and 0 instead.

C does however provide the header stdbool.h, which contains the macros bool, true and false:

  • The bool macro expands to _Bool.
  • The true macro expands to 1.
  • The false macro expands to 0.

This header can be used to write C code using the same type names as in C++. true/false is generally more readable than 1/0, so it is good practice to always use stdbool.h.


Please also note the following bugs in your code:

  • k=sizeof(word1) doesn't work because word1 is not an array but an array decayed into a pointer. You should be using strlen instead.
  • But your algorithm is all over the place... you quit the function if the first characters match, instead of comparing the whole string. The correct way to implement your own version of strcmp is rather something like this:

    int my_strcmp (const char* s1, const char* s2)
    {
      while (*s1 != '\0' && (*s1 == *s2))
      {
        s1++; 
        s2++;
      }
      return (int)*s1 - (int)*s2;
    }
    

(Note that this is a naive implementation, good enough for students but not good enough for standard library quality.)