4

What is wrong in this code?

#include <stdio.h>

bool func(char *, int);

void main()
{
   char *a = "Interview";
   if(func(a, 9))
   {
      printf("True");
   }
   else
   {
      printf("False");
   }
}

bool func(char *s, int len)
{
   if(len < 2)
      return true;
   else
      return s[0] == s[len-1] && func(&s[1], len-2);
}

I believe this function always returns TRUE. This is an interview question. But, when I try to compile this, it shows six errors...

4
  • 7
    You're missing #include <stdbool.h>. The return type of main shall be int. Commented Nov 7, 2012 at 16:45
  • 2
    you may use int instead of bool and 1/0 instead of true/false. or #define bool int, #define true 1, #define false 0 Commented Nov 7, 2012 at 16:48
  • 1
    syntax error : identifier 'func' Commented Nov 7, 2012 at 16:49
  • @JX yes i may, but it was an interview question.. so i can't make any correction on that Commented Nov 7, 2012 at 16:51

3 Answers 3

17

I'm going to guess it doesn't know what bool and true are. bool is not a primitive data type in C. You need an extra include:

#include <stdbool.h>

The second part of your question? Does it always return TRUE?

No:

When you come into the function, you'll skip the first return because your length is 9. So instead you'll return if true only if:

return s[0] == s[len-1] && func(&s[1], len-2)

Is true. You can skip the recursive logic here because it's not modifying your string. Just look at the first part:

s[0]     // This has to be 'I'
==       // We need so what we compare against has to be 'I' as well
s[len-1] // This will be 'w'

So... that's not going to return true... Who cares about ANDing (&&) the recursive part? I suspect the compiler will even optimize that out because everything here is hard coded.

Sign up to request clarification or add additional context in comments.

8 Comments

Correction, bool is not a primitive data type, but it is standard in C99 and following(hence stdbool )
please correct me... the particular function will always return TRUE or not??
because... s[len-1] && func(&s[1], len-2); at last func(&s[1], len-2); return TRUE, which means it become s[len-1] && true and anything except 0 is also known as TRUE. so thats why. pls correct me. if my logic is wrong.
@coders - right... so s[len-1] && true will be true, but it doesn't end there... you need to have s[0] == s[len-1] for it to be true as well. That can't happen. Check out the edit to my post regarding the logic
@Mike thanks !! but program showing error.fatal error C1083: Cannot open include file: 'stdbool.h': No such file or directory why so.. ??
|
6

You just have to include the right header.

#include <stdbool.h>

Or, you can use _Bool type, which don't need any inclusion. bool is just an alias from this type. By the way, don't forget to compile in C99.

Comments

-2

true and false are defined as 1 and 0, so you should do this:

#define true 1
#define false 0
int code() {
  //code
}

1 Comment

Re "true and false are defined as 1 and 0": Do you have a source?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.