Sandeep Singh
Sandeep Singh

Reputation: 5181

Can we verify whether a typedef has been defined or not

Suppose my program is:

typedef int MYINT;

int main()  
{  
    MYINT x = 5;  
    ........  
    do_something()    
    ........    
    /* I wanna test whether MYINT is defined or not */
    /* I can't use: ifdef (MYINT), since MYINT is not a macro */

    ........
    return 0;
}

Actually, I encountered this problem while I was using a cross-compiler for vxworks. The cross-compiler header file included: typedef int INT.

But, my stack's header file used:

 #ifndef INT  
 #define int INT

Can you please suggest how to test typedefs, whether they are defined previously or not?

Thanks in advance.

Upvotes: 11

Views: 11363

Answers (4)

BiGYaN
BiGYaN

Reputation: 7159

No, in C you cannot do this directly. However you can use #ifdef to check whether a name is already defined or not.

Upvotes: 1

Andy Johnson
Andy Johnson

Reputation: 8149

You can't do this with a typedef because C doesn't have any reflection capability.

All I can suggest is that you use #define as suggested by @larsmans.

#define MYINT int
...
#ifdef MYINT
  // MYINT is defined
#endif

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363547

No, this is not possible. Your best option is to use typedef yourself, since redefinition of a typedef is an error.

#ifdef INT
# error "No, we want to define INT!"
#endif

typedef int INT;  // error if previously typedef'd

(I suppose you didn't really want to #define int INT :)

Upvotes: 3

user703016
user703016

Reputation: 37945

I don't think you can.

typedef is just a typename alias for the compiler.

Upvotes: 6

Related Questions