Search code examples
cmacrosc-preprocessortypedef

Are typedef and #define the same in C?


I wonder if typedef and #define are the same in C. What are the differences between them?


Solution

  • No.

    #define is a preprocessor token: the compiler itself will never see it.
    typedef is a compiler token: the preprocessor does not care about it.

    You can use one or the other to achieve the same effect, but it's better to use the proper one for your needs

    #define MY_TYPE int
    typedef int My_Type;
    

    When things get "hairy", using the proper tool makes it right

    #define FX_TYPE void (*)(int)
    typedef void (*stdfx)(int);
    
    void fx_typ(stdfx fx); /* ok */
    void fx_def(FX_TYPE fx); /* error */