#typedef & #define

This is a discussion on #typedef & #define within the C++ Programming forums, part of the General Programming Boards category; what's the difference? --TING...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28

    #typedef & #define

    what's the difference?

    --TING

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Code:
    #define MAX_SIZE 80
    to generate a constant.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    #define - is substitution of the string performed by preprocessor
    typedef - alias of the type done by compiler
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 05:58 PM
  4. edit controls in dialogs
    By Homunculus in forum Windows Programming
    Replies: 10
    Last Post: 02-23-2006, 02:38 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21