-1

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);
}
  • 1
    `bool` is not a built-in type. Refer this: https://stackoverflow.com/questions/1921539/using-boolean-values-in-c – Eric Mar 27 '20 at 06:03
  • 2
    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 Mar 27 '20 at 06:04
  • 1
    `k=sizeof(word1)` should be `k=strlen(word1);` and also you have logic erors in your loop – M.M Mar 27 '20 at 07:04

2 Answers2

2

you have to do:

#include <stdbool.h> 

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

bahij zeineh
  • 228
  • 2
  • 11
1

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.)

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Thanks for the explanation, though I was trying to implement the string_compare function without using any actual string function and pointers. When I am passing same words, it gives me a value 1, when I am changing word2, it gives me value 0, as I am expecting. But the only point when I am getting wrong value is, when I am writing a word more in "word1", it still gives me value 1, however they are not equal and should give zero. I am adding the edited code in my question. – Gagan Batra Mar 28 '20 at 03:00
  • @GaganBatra So what is this code I posted here then? – Lundin Mar 30 '20 at 06:31
  • if you see my comment properly, I said “without using actual string functions and pointer” – Gagan Batra Mar 31 '20 at 14:31