Cannot return bool from function

10,942

Solution 1

You probably have just forgotten to #include <stdbool.h>.

However, you have a mismatch between name and behaviour of your function,

if((ascii_value<40)&&(ascii_value >= 30))

the decimal digits, 0-9, occupy the places 48-57 in the ASCII table, that's hexadecimal 0x30-0x39, so to match the name, you should test

if (ascii_value < 0x3A && ascii_value >= 0x30)

Solution 2

C does not have bool type, neither true or false. Use int and 0/1 values instead.

Solution 3

If your compiler supports, at least partially, the C99 standard (or C11, but that's not yet likely), add

#include <stdbool.h>

to the top of your source file to make bool, false, and true visible.

If it doesn't (Microsoft's support for C99 is not good), a workaround is:

typedef enum { false, true } bool;

This doesn't quite match the semantics of C99's bool (actually _Bool) type, but it's probably close enough.

Incidentally, you don't need an if/else statement in your function:

static bool is_ascii_value_of_digit(char ascii_value) {
    return ascii_value >= '0' && ascii_value <= '9';
}

bool values are values, and they can be stored and returned from functions just like any other values.

Another guideline: Don't compare boolean values to true or false, just test them directly. In a condition (such as in an if statement), any non-zero value is considered true, so this:

if (cond == true) ...

can fail if cond has non-zero value other than 1. Just write:

if (cond) ...

or, to test whether it's false:

if (!cond) ...

Recommended reading: section 9 of the comp.lang.c FAQ. (Further recommended reading: all the rest of the sections.)

Share:
10,942
user1133324
Author by

user1133324

Updated on June 04, 2022

Comments

  • user1133324
    user1133324 almost 2 years

    This is the piece of code in base C:

    static bool is_ascii_value_of_digit(char ascii_value){
    
        if((ascii_value<40)&&(ascii_value >= 30)){
            return true;
        }
        return false;
    }
    

    The avr studio gcc compiler is giving me error :

    ../calculator.h:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'is_ascii_value_of_digit'

    Similar errors are also evident in other functions. I do not know why this is happening. Spent an hour figuring out why and finally gave up. As far as I have seen online, my syntax is not the problem. Probably something that I am overlooking.

    Question RESOLVED !

    Thank you very much for your quick help as it saved me a lot of time. I was under the assumption that bool is a keyword in c.

  • Daniel Fischer
    Daniel Fischer over 12 years
    It does since 1999, all you need is <stdbool.h>.
  • Timothy Jones
    Timothy Jones over 12 years
    or just return isdigit(ascii_value) :)
  • Daniel Fischer
    Daniel Fischer over 12 years
    Yeah :D But I was under the impression do-it-yourself was the point here.
  • tomlogic
    tomlogic over 12 years
    Clearer: if (ascii_value >= '0' && ascii_value <= '9') (assuming you're trying to do this without isdigit()).
  • Keith Thompson
    Keith Thompson over 12 years
    void main() is wrong; it should be int main(void). b==False is better written as !b. Read section 9 of the comp.lang.c FAQ.
  • Keith Thompson
    Keith Thompson over 12 years
    @tomlogic: That's not only clearer, it's more portable (in EBCDIC, '0 .. '9' are 0xF0 .. 0xF9. (C does guarantee that the values are contiguous, a guarantee it doesn't make for letters.)
  • remmy
    remmy over 10 years
    I wouldn't say that it's "wrong", it's just not what you want most of the time. Sometimes you have to have main be of type void, for example many compilers for embedded systems require it.
  • Admin
    Admin over 10 years
    @Demizey: Many compilers and libraries for embedded systems are not standards conforming and in the context of ANSI C they are wrong.
  • remmy
    remmy almost 10 years
    @oscode: False. In hosted implementations you need to return a value, but freestanding implementations, which are very common in embedded environments, have no such requirements and void main() is just fine.