It is not possible to use a typedef to alias a preprocessor directive. typedef
is a keyword, handled by the compiler, not the preprocessor. The preprocessor removes comments from your code and replaces the preprocessor directives such as #include
with the appropriate output. When you compile a c program there are really three things that happen:
- First, the preprocessor removes the comments from your code and replaces the corresponding preprocessor directives such as
#include
with the appropriate preprocessor output.
- Then, the compiler takes the preprocessed code and outputs the compiled code into object files.
- Lastly, the linker processes the compiled code, combines the object files into one executable file if needed and links the executable file with the necessary libraries.
So although you can't use typedef to alias a preprocessor directive, you can (although you really probably shouldn't) do something like this:
uselessTypedef.h
int NUMBER
uselessTypedef.c
#include <stdio.h>
// note the whitespace between the typedef, #include and the semicolon
// typedef #include "uselessTypedef.h"; doesn't compile because of the way the preprocessor and compiler handle things
typedef
#include "uselessTypedef.h"
;
NUMBER main(NUMBER argc, char* argv[])
{
NUMBER myint = 12345;
printf("value of NUMBER myint: %d\n", myint);
return 0;
}
this code compiles and prints value of NUMBER myint: %d\n
The compiler never sees the following code:
typedef
#include "uselessTypedef.h"
;
Instead, the compiler sees this:
typedef
int NUMBER
;
The compiler ignores whitespace, so that code is equivelent to typedef int NUMBER;