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
When it comes to compilation, your source code pass through a lot of step. In those step, there is pre-proccessing.
The preprocessor is program that runs before the compiler, and execute all the #
started instructions, like #include
, #define
, etc..
In your particular case, #define WORD newword
is a directive that says : "Replace all occurrence of WORD with newword", before even trying to compile the program.
If you want to see it in action, try to run cpp file.c
and examine the output to understand what it does.
file.c
#define WORD "newword"
int main()
{
printf("this is word: %s\n", WORD);
return 0;
}
will become after cpp runs
int main()
{
printf("this is word: %s\n", "newword");
return 0;
}
typedef, on the other hand, are used to say "if I talk about Type
, understand that I meant struct more_complex_type
" It is used at compile time, and will stay the same before and after the cpp
pass.