+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Full Member level 3
    Points: 1,605, Level: 9
    chandu.kurapati's Avatar
    Join Date
    Oct 2013
    Location
    Bengaluru, India
    Posts
    186
    Helped
    5 / 5
    Points
    1,605
    Level
    9

    Question Related to the typecasting from bool to char

    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
    Reagrds,
    Chandu.Kurapati

    Did you find this post helpful? Click: Yes

    •   Alt7th December 2017, 05:54

      advertising

        
       

  2. #2
    Advanced Member level 4
    Points: 8,055, Level: 21
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,443
    Helped
    331 / 331
    Points
    8,055
    Level
    21

    Re: Question Related to the typecasting from bool to char

    I think it is the same.
    Love me or hate me. All infractions will be ignored.



    •   Alt7th December 2017, 09:30

      advertising

        
       

  3. #3
    Super Moderator
    Points: 237,321, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    41,068
    Helped
    12540 / 12540
    Points
    237,321
    Level
    100

    Re: Question Related to the typecasting from bool to char

    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.



    •   Alt7th December 2017, 09:43

      advertising

        
       

  4. #4
    Super Moderator
    Points: 8,139, Level: 21

    Join Date
    Jun 2009
    Location
    Japan
    Posts
    1,054
    Helped
    279 / 279
    Points
    8,139
    Level
    21

    Re: Question Related to the typecasting from bool to char

    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.



  5. #5
    Super Moderator
    Points: 24,714, Level: 38
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    7,432
    Helped
    925 / 925
    Points
    24,714
    Level
    38
    Blog Entries
    5

    Re: Question Related to the typecasting from bool to char

    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).
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--