NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Problem with Boolean type

April 26, 2012
by HexManiac
HexManiac's Avatar

I'm having one tiny problem at the moment:

I declare a function like...

void blink(bool changestate)
{
    if(changestate)
    {
        PORTC |= (1<<PC4);
    }
    else
    {
        PORTC &= ~(1<<PC4);
    }
}

and compiler says... test_bool.c:15: error: expected ')' before 'changestate'

If I change bool to int it works as expected. Can anyone explain why I get this error, and how I can fix?

April 26, 2012
by pcbolt
pcbolt's Avatar

HexManiac -

I can't be certain but the AVR GCC compiler may not have boolean types declared. There are some minor differences like that in AVR programming for instance the "double" type floating point variable is actually the same as a "float" type. Usually boolean type variables are byte size (8 -bit) anyway so the equivalent type to declare would be "uint8_t".

April 26, 2012
by HexManiac
HexManiac's Avatar

Thanks for the quick reply pcbolt!

You may be right about the 'bool' type not being declared to the compiler, I'll look into it, but as it's the simplest possible variable type you'd expect it to be implemented by default.

I'm pretty sure a boolean variable only needs one BIT of storage, because it only represents one BIT of information.

April 26, 2012
by Ralphxyz
Ralphxyz's Avatar

This is interesting.

I googled GCC bool

The best summary is:

C99 has it in stdbool.h, but in C90 it must be defined as a typedef or enum.

typedef int bool;
#define TRUE  1
#define FALSE 0

bool f = FALSE;
if (f) { ... }
Alternatively:

typedef enum { FALSE, TRUE } boolean;

boolean b = FALSE;
if (b) { ... }

It is said that GCC has limited support for C99 (the standard).

Of course if it is defined as a int it is 8 bits not one.

Ralph

Ralph

April 26, 2012
by pcbolt
pcbolt's Avatar

@HexManiac

Booleans need only one bit, but I'm pretty certain the processor will allocate at least 8-bits for it. The processor can only load/store data using byte values addresses. So even if you could ask for a one-bit variable, you're getting at least 8.

@Ralph

Isn't an "int" 16-bits in AVR?

April 26, 2012
by Ralphxyz
Ralphxyz's Avatar

Isn't an "int" 16-bits in AVR?

I googled GCC int it and now really confused.

http://crasseux.com/books/ctutorial/Integer-variables.html

C99 uint8_t is always exactly eight bits wide.

Sure get some fascinating reading.

"int" literally is not referenced (at least I did not find a direct reference or definition).

I believe the C99 standard is where the uintX_t type was established/confirmed.

So I guess the best one could do would be:

typedef uint8_t bool;
#define TRUE  1
#define FALSE 0

I am a little lost on the "Alternative method" listed above.

Ralph

April 26, 2012
by BobaMosfet
BobaMosfet's Avatar

In standard C, a boolean is usually based off an 8-bit wide enumerated type.

Try using 'boolean' instead of 'bool'.

Try defining it yourself 'safely':

#ifndef bool
typedef uint8_t bool;
#endif bool

Please forgive any typos, have an awful headache right now.

BM

April 27, 2012
by HexManiac
HexManiac's Avatar

Thanks for all your responses, using unit8_t does seem to be the simplest option.

May 01, 2012
by bobpk12
bobpk12's Avatar

To use "bool" type I found I had to add the following include file.

include <stdbool.h>

May 01, 2012
by HexManiac
HexManiac's Avatar

Thanks! Problem solved!

May 01, 2012
by BobaMosfet
BobaMosfet's Avatar

Ideally, you should just spend some time going through the libraries and familiarizing yourself with what is there-- a C reference will generally tell you what libraries various standard types are in.

BM

May 03, 2012
by HexManiac
HexManiac's Avatar

Thanks for the advice BM, I will do that. I've been writing for Win32 for so long I forgot the basics lol!

Post a Reply

Please log in to post a reply.

Did you know that using an alternating excitation voltage can reduce the noise in many sensor measurements? Learn more...