Monday 15 April 2013

macros - typedef vs #define for datatypes in c -


In a few years ago in an interview, I was asked how to know about implementing a Boolean datatype in C. The two most common
  define the # BOOL unsigned char   

or typedefs: < / P> ex> typedef BOOL unsigned char;

So I was thinking that either the exact advantage of using preprocessor macros or typefaces and trade-offs are? I usually make myself a datatype like "UIT16," (for unsigned shorts) and "uchararp," (for unsigned char points).

Thanks

The most natural answer is to define an enumerated type:

  typingfnf {false = 0, true} BOOL;     

It is important to note, however, that we have declared this type of code BOOL Behaves slightly differently> _Bool type (most commonly defined by bool macro in stdbool.h ).

For example:

  int main (zero) {BOOL B; // our type bool B; // type C99 B = -true; // -1B = - tru; // 1b = true + true; // 2b = true + true; // return 1; }   

acts as per the rules of C99 bool , while our make-do BOOL is a regular integer . This is due to the fact that a variable of type enum is actually a int .

If you are using C99, there is actually no valid reason to define bool in stdbool.h instead of this earth To roll on your own type

If you are on the first standard, a typedef'd enum gets it as well, unfortunately.

No comments:

Post a Comment