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 post that states
#defineis a preprocessor token: the compiler itself will never see it.
typedefis a compiler token: the preprocessor does not care about it.
Could anyone please explain this with a little more detail. I am confused with the term preprocessor here.
The pre-processor is a program that runs before the compiler and essentially performs text substitution. When you write:
The pre-processor take that file as input, does it’s thing, and outputs:
And then the compiler does its thing with the pre-processed output.
typedefon the other hand is a construct that the compiler understands. When you write:The compiler knows that
uint32is actually an alias forunsigned int. This alias is handled by the compiler itself and involves a bit more logic than simple text substitution. This becomes obvious with a simple example:If a
typedefwere a simple text substitution mechanism (like the pre-processor is) then that would work, but it is not allowed and will fail to compile.