Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question Related to the typecasting from bool to char

Status
Not open for further replies.

chandu.kurapati

Full Member level 3
Full Member level 3
Joined
Oct 31, 2013
Messages
186
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
Bengaluru, India
Activity points
2,778
Hello,

What happens when we typecast the bool variable to char and vice versa char variable to bool??

Anyone please explain with example.

Thanks & Regards,
CHandu.K
 

Can't be answered generally. Bool e.g. defaults to 32 bit with some 32-bit compilers. On PIC processors, bool might be a single bit variable type.
 

Hello Easyrider!

I think for the data size, you're right, although FvM says that some compiler default to 32 bits.

What happens when we typecast the bool variable to char and vice versa char variable to bool??

In general, the cast does nothing except giving a hint to the compiler on how to interpret it.
The data itself does not change. For example, if you read a stream of uint8 from the serial port
and want to print if with printf, it simply does nothing, but will tell the comiler to interpriet the
uint8 * stream as char *, and that's it. Not even 1 bit of the stream will change.

Now that's the same for bools. If they default to uint8, then a cast to bool tells the compiler to
interpret it as a bool.
And if you have a bool returning function like bool IsAscii(char c);, then if you have these statements:
uint8 a;
a = IsAscii('a');
then some compiler will start yelling because you assign a bool to a variable which is uint8, (although
it's the same). And some other compiler will not even issue a warning.

Dora.
 

What happens when we typecast the bool variable to char and vice versa char variable to bool?

The "vice versa" means lose of information, any value different from Zero would yield true, therefore it is not exactly a typecasting opperation but rather a logical evaluation (the inverse opperation would not give the original value).
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top