473,573 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting around lack of bool type support

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.
Dec 29 '07 #1
15 4320
On Dec 29, 2:21 am, ssylee <staniga...@gma il.comwrote:
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>
Dec 29 '07 #2
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
Dec 29 '07 #3
On Dec 28, 4:21 pm, ssylee <staniga...@gma il.comwrote:
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
Dec 29 '07 #4
Ivan Novick <iv**@0x4849.ne twrites:
>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.
Dec 29 '07 #5
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.

Dec 29 '07 #6
On Dec 28, 6:53*pm, ssylee <staniga...@gma il.comwrote:
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).
Dec 29 '07 #7
Ivan Novick <iv**@0x4849.ne twrites:
[...]
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_Keit h) <ks***@mib.or g>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 29 '07 #8

"user923005 " <dc*****@connx. comwrote 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

Dec 29 '07 #9
"ssylee" <st********@gma il.comwrote 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

Dec 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17520
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which...
12
2673
by: Gnolen | last post by:
Hi, I am really getting crazy here! I just do not get why this happens with the borders of the td/tr! I just want a border on the bottom of the rows(or td) but I just can't do it!!! I have tried so many different ways but I just thought this would do it: td {border-bottom: #000000 1px solid;} But no! What am I doing wrong? Because I can...
18
2873
by: Marcin Kalicinski | last post by:
Hi, Why is there void* conversion to check for std::istream failure bit? Why not a conversion to bool? When I try to return an istream from a function that actually returns bool, I get the following warning (in VC .NET): warning C4800: 'void *' : forcing value to bool 'true' or 'false' (performance warning)
16
4657
by: raj | last post by:
Hi, I saw it mentioned that "int" is the fastest data-type for use in C ,that is data storage/retrieval would be the fastest if I use int among the following 4 situations in a 32 bit machine with 4-byte ints: int m; bool m; // assuming I use C++ char m; unsigned char m;
8
4482
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working with weird stuff, just strings, int, bools and DataTime values) My fields values for this record, for convenience are also in an array of objects. ...
3
3069
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are displayed. Can anybody know this issue and help me to resolve this. In past i received the response saying that i should download the image first...
0
3702
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
3
2271
by: Greg | last post by:
My problem is that values in the bool column of a datagrid are only being updated to the database once the focus of the bool cell is lost. This is completely counter-intuitative. When a user clicks on a bool type cell in a datagrid and sees a check mark put in / removed, then he/she would expect this to be reflected in the databsae...
76
4846
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there...
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8033
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7796
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2224
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.