Thread: typedefining the BOOL as unsigned char

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    Unhappy typedefining the BOOL as unsigned char

    Hi can anyone please tell me how can i typedef the BOOL to unsigned char , if it is defined , but if we havt define the BOOL as unsigned char then it has to take the defualt bool (windows BOOL).


    [code]:

    #if defined BOOL

    typedef unsigned char BOOL


    #elif

    ----


    [code]


    I want the codes here plz help me.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    #ifndef BOOL
    typedef unsigned char BOOL;
    #endif
    You should get Windows' BOOL if you include any of Windows-specific headers.

    Edit: Missing semicolon, duh.
    Last edited by msh; 11-14-2011 at 03:05 AM.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Thanks for your help. but my requirement is like this :

    if i define BOOL as unsigned char then i have to use the same all over the programme , if BOOL is not defined as unsigned char then it has to take the defualt windows BOOL.

    Looking forward for the replay.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ushacy View Post
    Thanks for your help. but my requirement is like this :

    if i define BOOL as unsigned char then i have to use the same all over the programme , if BOOL is not defined as unsigned char then it has to take the defualt windows BOOL.

    Looking forward for the replay.
    First... you will regret this. You will run into all kinds of type definition errors... it won't use the windows version intellegently, it will simply tell you that you have redefined the type, error off and stop compiling.

    Windows BOOL type is an integer for a reason... you are testing for 0 vs !0 but some windows functions can and do return large numbers for the !0 case. (Registry functions are notorious for this)

    That said...
    Code:
    #ifndef _BOOL
    typedef unsigned char BOOL;
    #define _BOOL
    #endif

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also: processors evaluate ints faster than 1 byte chars, because they do not load single bytes, they load multiples of 4. And because of paging any apparent memory saving is going to be illusory. So there is no advantage to using an unsigned char, but potentially some disadvantages.

    If you have a choice, use an int or unsigned int. Unsigned char is just silly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @MK27 ... one for the history books, my friend... Looks like we actually agree on something!

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Ya you are right ,, if i try to change the type of windows data types it will through a erro but here my requirement is like this ... i have to redefine the windows data type to my own definition as BOOL to unsigned char and i have to use this all over the programme.


    i have tried your way of doing :

    Code:
    #ifdef
    _BOOL
    typedef
    unsignedchar BOOL;
    #define
    _BOOL
    #endif [code]

    but its is throuing an error and it s not redefining please help me in this issue.

    Thanks in advance

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    [QUOTE=ushacy;1069661]Ya you are right ,, if i try to change the type of windows data types it will through a erro but here my requirement is like this ... i have to redefine the windows data type to my own definition as BOOL to unsigned char and i have to use this all over the programme.


    i have tried your way of doing :

    Code:
    #ifndef
    _BOOL
    typedef
    unsigned char BOOL;
    #define
    _BOOL
    #endif [code]

    but its is throuing an error and it s not redefining please help me in this issue.

    Thanks in advance

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's what I told you in the first message... you can't do that...

    Windows defines it's own BOOL type... just use it.


    The only thing you can do is define a secondary type...
    Code:
    #ifndef _CBOOL
    typedef unsigned char cBOOL;
    #define _CBOOL
    #endif
    And no you cannot put in line breaks like you did in message #8, that will cause it's own set of errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned : 1; or bool ? C
    By Hennet in forum C Programming
    Replies: 15
    Last Post: 06-24-2011, 12:11 AM
  2. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  3. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  4. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM