I am trying to understand the difference between a typedef and define. There are a lot of good posts specially at this previous question on SO, however I can\'t understand the p
The C and C++ preprocessors are a logical phase in the compilation that occurs very early on. The preprocessor converts a source file into a translation unit by including the text of header files that are specified by #include
, by conditionally eliminating parts of the files (#if
, #elif
, #else
, #endif
, and variants #ifdef
and #ifndef
), and by doing macro substitutions. The macros are defined via #define
; the macros can be detected by the preprocessor scanning the source.
The preprocessor eliminates most lines that start with #
(it only leaves #line
directives behind and its own abbreviated variants on #line
behind to tell the compiler where the source came from). The compiler proper then sees the result of the pre-processing and compiles the source code that is defined.
The preprocessor would not normally modify the word typedef
. An insane programmer could define a macro for typedef
and the preprocessor might not care; the programmer could not legitimately include any system header while there is a macro defined with the same name as any keyword in the language. Otherwise, though, typedef
is a problem for the compiler, not the preprocessor. So, too, is sizeof()
; that is not something the preprocessor understands.
There are usually compiler options to allow you to see the pre-processed output. For gcc
, the options are -E
and -P
.