Difference between Typedef and Define...in C ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ngpbalaji
    New Member
    • Apr 2007
    • 26

    Difference between Typedef and Define...in C ?

    whats the Difference between Typedef and Define in C?
    we can Use both for the case below...

    #define INT32 int

    typedef int INT32;

    for both the case actually what will happen when compiling and running?

    And is there any specific use for typedef .. If yes give me Examples ?

    Thanks & Regards
    -Balaji
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    I don't think you can do

    #typedef lengthof(exp) ((sizeof((exp)) )/sizeof((*(exp)) ))

    typedef only allows you to redefine types where as #define will let you define anything.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by ngpbalaji
      whats the Difference between Typedef and Define in C?
      we can Use both for the case below...

      #define INT32 int

      typedef int INT32;

      for both the case actually what will happen when compiling and running?

      And is there any specific use for typedef .. If yes give me Examples ?

      Thanks & Regards
      -Balaji
      A #define is just a macro, i.e. it will be processed/expanded by the preprocessor.
      When the actual C compiler starts doing its work there will be no sign of any
      INT32 anymore. The typedef language facility is handled by the C compiler
      itself: it introduces another name for a type. It's an awful hack actually: when
      the C compiler parses that name in the right context the name is supposed
      the alias for the type, otherwise it's supposed to be an identifier name:
      Code:
      typedef int t;
      t t(t x) { int t; ... }
      kind regards,

      Jos

      Comment

      • ngpbalaji
        New Member
        • Apr 2007
        • 26

        #4
        Originally posted by RedSon
        I don't think you can do

        #typedef lengthof(exp) ((sizeof((exp)) )/sizeof((*(exp)) ))

        typedef only allows you to redefine types where as #define will let you define anything.

        The define will replace all the Macros before compile..but typedef will redefine types after compilation ...
        Is it correct?
        can u explain me on this?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by ngpbalaji
          The define will replace all the Macros before compile..but typedef will redefine types after compilation ...
          Is it correct?
          can u explain me on this?
          Correct about the #define part. A typedef is just a new name for an already
          existing type. defines are handled by the preprocessor while typedefs are
          handled by the C compiler itself.

          kind regards,

          Jos

          Comment

          • ngpbalaji
            New Member
            • Apr 2007
            • 26

            #6
            Originally posted by JosAH
            Correct about the #define part. A typedef is just a new name for an already
            existing type. defines are handled by the preprocessor while typedefs are
            handled by the C compiler itself.

            kind regards,

            Jos

            Thanks for the kind Reply...
            Regards
            Balaji

            Comment

            • mkyadav206
              New Member
              • Feb 2015
              • 1

              #7
              1.The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, like you can define 1 as ONE etc.

              2.#define should not be terminated with semicolon, but typedef should be terminated with semicolon.

              3.he typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor.

              4. #define will just copy-paste the definition values at the point of use, while typedef is actual definition of a new type

              5.typedef follows the scope rule which mean if a new type is defined in a scope(inside a function), then the new type name will only be visible till the scope is there.

              Here I got exact answer.. Typedef vs Define in c

              Comment

              • Sherin
                New Member
                • Jan 2020
                • 77

                #8
                Typedef :

                A typedef defines a new name for existing types and does not introduce a new type. It is the (partial storage-class specifier) compiler directive mainly use with user-defined data types (structure, union or enum) to reduce their complexity and increase code readability and portability.

                Syntax:

                typedef type NewTypeName;

                example

                int main()
                {
                UnsignedInt data1, data2;

                data1 = 100;

                data2 = 200;

                printf("%d %d ",data1,dat a2);

                return 0;
                }

                Output: 100 200



                define

                A #define is a preprocessor directive and it replaces the value before compiling the code. One of the major problems with the macro that there is no type checking. Generally, the macro is used to create the alias, in C language macro is also used as a file guard.

                example

                int main()
                {
                printf("%d ", Value);

                return 0;
                }

                Output: 10

                Comment

                Working...