Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getting around lack of bool type support

Reply
Thread Tools

getting around lack of bool type support

 
 
ssylee
Guest
Posts: n/a
 
      12-29-2007
I am using mikroC to program some microcontroller code in C. However I
have come across of potential problem of bool type not being supported
from the compiler complaint on how I declared type bool variable. From
a forum thread that I've pulled out from mikroBasic, I realized that
bool type isn't fully supported. When programming in mikroC, I have
been getting an error message of "invalid expression" when I declared
a bool type variable, "specifier needed" when I declared the bool type
variable within a function parameter and also when I declared the bool
type variable as a file scope. I'm not sure if there are any mikroC
users on this mailing list, but would much appreciate any comments or
suggestions. Thanks.
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      12-29-2007
On Dec 29, 2:21 am, ssylee <(E-Mail Removed)> wrote:
> I am using mikroC to program some microcontroller code in C. However I
> have come across of potential problem of bool type not being supported
> from the compiler complaint on how I declared type bool variable. From
> a forum thread that I've pulled out from mikroBasic, I realized that
> bool type isn't fully supported. When programming in mikroC, I have
> been getting an error message of "invalid expression" when I declared
> a bool type variable, "specifier needed" when I declared the bool type
> variable within a function parameter and also when I declared the bool
> type variable as a file scope. I'm not sure if there are any mikroC
> users on this mailing list, but would much appreciate any comments or
> suggestions. Thanks.


#include <stdbool.h>
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      12-29-2007
ssylee wrote, On 29/12/07 00:21:
> I am using mikroC to program some microcontroller code in C. However I
> have come across of potential problem of bool type not being supported
> from the compiler complaint on how I declared type bool variable. From
> a forum thread that I've pulled out from mikroBasic, I realized that
> bool type isn't fully supported. When programming in mikroC, I have
> been getting an error message of "invalid expression" when I declared
> a bool type variable, "specifier needed" when I declared the bool type
> variable within a function parameter and also when I declared the bool
> type variable as a file scope. I'm not sure if there are any mikroC
> users on this mailing list, but would much appreciate any comments or
> suggestions. Thanks.


I know nothing of mikroC (or microBasic) but I *do* know that the
boolean type (spelled _Bool) was only added to C in the 1999 standard
which is not implemented fully by most compilers (and a significant
number do not implement it at all). Further, it is only spelt "bool" if
you have included stdbool.h

In C you can use any integral type for boolean work as long as you
understand that 0 if false and *any* non-zero value is true, so don't
define a constant named "true" or "TRUE" and do comparisons against it.
--
Flash Gordon
 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      12-29-2007
On Dec 28, 4:21 pm, ssylee <(E-Mail Removed)> wrote:
> I am using mikroC to program some microcontroller code in C. However I
> have come across of potential problem of bool type not being supported
> from the compiler complaint on how I declared type bool variable. From
> a forum thread that I've pulled out from mikroBasic, I realized that
> bool type isn't fully supported. When programming in mikroC, I have
> been getting an error message of "invalid expression" when I declared
> a bool type variable, "specifier needed" when I declared the bool type
> variable within a function parameter and also when I declared the bool
> type variable as a file scope. I'm not sure if there are any mikroC
> users on this mailing list, but would much appreciate any comments or
> suggestions. Thanks.


If you are trying to get existing code to compile and don't have bool
on your system try typedefing it.

typedef short bool
#define true 1
#define false 0

Regards,
Ivan Novick
http://www.0x4849.net
 
Reply With Quote
 
Chris McDonald
Guest
Posts: n/a
 
      12-29-2007
Ivan Novick <(E-Mail Removed)> writes:

>If you are trying to get existing code to compile and don't have bool
>on your system try typedefing it.


>typedef short bool
>#define true 1
>#define false 0


(Serious question) why have you chosen to use a short (not a char, not an int)?
Thanks,

--
Chris.
 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      12-29-2007
Thank you for the responses. I have used the method of defining 1 and
0 to true and false and got around that. The reason why I used short
instead of char or int is probably b/c of avoiding unnecessary memory
consumption, although the difference is nearly negligible.

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      12-29-2007
On Dec 28, 6:53*pm, ssylee <(E-Mail Removed)> wrote:
> Thank you for the responses. I have used the method of defining 1 and
> 0 to true and false and got around that. The reason why I used short
> instead of char or int is probably b/c of avoiding unnecessary memory
> consumption, although the difference is nearly negligible.


On many systems, char is smaller than short.
And char will never be larger than short.

Anyway {for pre-C99 at least}, it's a FAQ:

9.1: What is the right type to use for Boolean values in C? Why
isn't it a standard type? Should I use #defines or enums for
the true and false values?

A: C does not provide a standard Boolean type, in part because
picking one involves a space/time tradeoff which can best be
decided by the programmer. (Using an int may be faster, while
using char may save data space. Smaller types may make the
generated code bigger or slower, though, if they require lots
of
conversions to and from int.)

The choice between #defines and enumeration constants for the
true/false values is arbitrary and not terribly interesting
(see
also questions 2.22 and 17.10). Use any of

#define TRUE 1 #define YES 1
#define FALSE 0 #define NO 0

enum bool {false, true}; enum bool {no, yes};

or use raw 1 and 0, as long as you are consistent within one
program or project. (An enumeration may be preferable if your
debugger shows the names of enumeration constants when
examining
variables.)

Some people prefer variants like

#define TRUE (1==1)
#define FALSE (!TRUE)

or define "helper" macros such as

#define Istrue(e) ((e) != 0)

These don't buy anything (see question 9.2 below; see also
questions 5.12 and 10.2).
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-29-2007
Ivan Novick <(E-Mail Removed)> writes:
[...]
> If you are trying to get existing code to compile and don't have bool
> on your system try typedefing it.
>
> typedef short bool
> #define true 1
> #define false 0


I like this:

typedef enum { false, true } bool;

but of course there are many other possibilities.

See also section 9 of the comp.lang.c FAQ, <http://www.c-faq.com>.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      12-29-2007

"user923005" <(E-Mail Removed)> wrote in message

> #define TRUE 1 #define YES 1
> #define FALSE 0 #define NO 0


> #define TRUE -1 is also very neat.


A single set bit is -1 in two's complement notation, but the real advantage
is now we can say
~TRUE == FALSE.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      12-29-2007
"ssylee" <(E-Mail Removed)> wrote in message
>
> Thank you for the responses. I have used the method of defining 1 and
> 0 to true and false and got around that. The reason why I used short
> instead of char or int is probably b/c of avoiding unnecessary memory
> consumption, although the difference is nearly negligible.
>

In that case use int. I'm a firm believer in ints with everything, unless
the case for another type is overwhelming. There are lots of advantages in
having only one integer type kicking about the system.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
enum promote to bool type rather than Integer type? FE C++ 6 08-04-2009 03:21 PM
Name-brand DVD decks lack support RichA DVD Video 1 07-20-2005 02:05 PM
Lack of Support Stephen Boursy Firefox 4 03-11-2005 04:37 PM
Spam and ISP lack of support Silverback Computer Support 11 02-06-2005 10:21 PM
Working around a lack of 'goto' in python Brett Python 13 03-07-2004 08:49 PM



Advertisments