Are typedef
's handled by C's preprocessor?
That is, is typedef int foo_t;
the same as #define foo_t int
?
Are typedef
's handled by C's preprocessor?
That is, is typedef int foo_t;
the same as #define foo_t int
?
No, because that type of replacement won't work with more complex types. For instance:
typedef int tenInts[10];
tenInts myIntArray;
The declaration of myIntArray
is equivalent to:
int myIntArray[10];
To do this as a macro you'd have to make it a function-style, so it can insert the variable name in the middle of the expansion.
#define tenInts(var) int var[10]
and use it like:
tenInts(myIntArray);
This also wouldn't work for declaring multiple variables. You'd have to write
tenInts(myArray1);
tenints(myArray2);
You can't write
tenInts myArray1, myArray2;
like you can with a typedef
.
No.
As an example,
typedef int *int_ptr1;
#define int_ptr2 int *
Then in:
int_ptr1 a, b;
int_ptr2 c, d;
a
and b
are both pointers to int
. c
is also a pointer to int
, but d
is an int
.
No ,typedef
( being a C keyword) is interpreted by compiler not by pre-processor whereas #define
is processed by pre-processor.
No - the C preprocessor doesn't handle typedefs. The C preprocessor does not recognize the syntax of the C language; it will not know what is and is not a typedef.
The #define is strictly a text replacement of "int" for "foo_t"; the replacements done by the preprocessor occur prior to compilation.