Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we verify whether a typedef has been defined or not

Tags:

c

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.

like image 329
Sandeep Singh Avatar asked Apr 22 '11 11:04

Sandeep Singh


People also ask

Is typedef same as define?

typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.

Can you redefine typedef?

Using typedef redeclaration, you can redefine a name that is a previous typedef name in the same scope to refer to the same type. For example: typedef char AChar; typedef char AChar; The typedef redeclaration feature can be enabled by any extended language level.

Does typedef have scope?

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

Is typedef the same as using?

They are largely the same, except that: The alias declaration is compatible with templates, whereas the C style typedef is not.


1 Answers

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
like image 80
Andy Johnson Avatar answered Oct 05 '22 16:10

Andy Johnson