Thread: #typedef & #define

  1. #1
    Registered User
    Join Date
    Mar 2008
    vancouver bc

    #typedef & #define

    what's the difference?


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Cheap and easy answer: #define exists, #typedef does not.

    Real answer: typedef gives a new name to an existing type, for one reason or another. For example, if you look at cstdint (the header file, or stdint.h), you'll see a bunch of typedef's. The reason being, sometimes people need to know that they're using, say, a 32-bit integer; they can declare their variable as "int_32t" and know that the compiler has typedef'ed that to whatever the appropriate type is (int, or long, or whatever).

    #define is just search-and-replace. Every appearance of the first token is replaced by whatever comes after it. It can do more than just types, but macros and constants. Common usage (in C, not necessarily C++) would be something like
    #define MAX_SIZE 80
    to generate a constant.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    #define - is substitution of the string performed by preprocessor
    typedef - alias of the type done by compiler
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  3. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  4. edit controls in dialogs
    By Homunculus in forum Windows Programming
    Replies: 10
    Last Post: 02-23-2006, 03:38 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM