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.
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.
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.
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.
They are largely the same, except that: The alias declaration is compatible with templates, whereas the C style typedef is not.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With