Thread: Standard typedefs ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    Standard typedefs ...

    Hi, why do people use certain typedefs for different things? like I think I saw one for "int" and stuff. I can understand one for "unsigned double long float character" (i made that one up by the way), and perhaps for BOOL, TRUE & FALSE, but .... yeah, I just found it odd is all.

    so anyone know why?

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    to make C look like something they are comfortable with.
    I've come across the following more than once:
    Code:
    #define begin {
    #define end }
    #define integer int
    #define real float
    #define procedure void
    // etc...
    which would lead to something like
    Code:
    procedure doSomething(integer a)
    begin
      real b;
      // do something more
    end
    which would look familiar to Pascal or Modula programmers.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's for added flexibilty...
    This way writing the code would become easier to you.

    This is why C is such a nice language
    none...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > to make C look like something they are comfortable with.
    I'm sure you jest, but ugh!!!!!
    http://c-faq.com/cpp/slm.html

    > Hi, why do people use certain typedefs for different things?
    Readability is one issue - function pointers in particular are unweildy beasts otherwise.

    Portability, say you have
    typedef int colour_t;
    Which on another system you have to do
    typedef unsigned long colour_t;

    If you'd used colour_t properly, this would be a much easier change than hunting down every int, and deciding whether it needed changing to unsigned long.

    Also, typedefs can do things which #defines can't.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Example from my desk: We have a client-server system that executes batch jobs. Each job can have a number of steps, each step processes records, each record processed has a return code. Since the system was developed, this code has always been an int. Batch functions return ints, the servers handle ints, statistic modules work with the ints, databases store the ints... until recently, we added a feature and found that ints were not enough. We needed 64-bit numbers.

    If we had properly typedef'd it like this

    Code:
    typedef int RECORD_RETURN_CODE;
    We would have been in heaven.

    Code:
    typedef __int64 RECORD_RETURN_CODE;
    One little change, maybe adjust the database and some minor display issues... done.

    Instead we had to go through every single function and look at every single int and check if it was an int or maybe the returncode of one of those functions.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Salem
    > to make C look like something they are comfortable with.
    I'm sure you jest, but ugh!!!!!
    sadly I'm not

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Heh, it's the Secret to Better C
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard efficiency
    By Jorl17 in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 11:48 AM
  2. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM