Navigation



4.6 — Typedefs

Typedefs allow the programmer to create an alias for a data type, and use the aliased name instead of the actual type name. To declare a typedef, simply use the typedef keyword, followed by the type to alias, followed by the alias name:

typedef long miles; // define miles as an alias for long

// The following two statements are equivalent:
long nDistance;
miles nDistance;

A typedef does not define new type, but is just another name for an existing type. A typedef can be used anywhere a regular type can be used.

Typedefs are used mainly for documentation and legibility purposes. Data type names such as char, int, long, double, and bool are good for describing what type of variable something is, but more often we want to know what purpose a variable serves. In the above example, long nDistance does not give us any clue what units nDistance is holding. Is it inches, feet, miles, meters, or some other unit? miles nDistance makes it clear what the unit of nDistance is.

This is also true of function return types. Which of the following is easier to decipher?

typedef int testScore;

int GradeTest();
testScore GradeTest();

What is the first GradeTest() returning? A grade? The number of questions missed? The student’s ID number? An error code? Who knows! Int does not tell us anything. Using a return type of testScore makes it obvious that the function is returning a type that represents a test score.

Furthermore, typedefs allow you to change the underlying type of an object without having to change lots of code. For example, if you were using a short to hold a student’s ID number, but then decided you needed a long instead, you’d have to comb through lots of code and replace short with long. It would probably be difficult to figure out which shorts were being used to hold ID numbers and which were being used for other purposes.

However, with a typedef, all you have to do is change typedef short studentID to typedef long studentID and you’re done. Precaution is mandatory when changing the type of a typedef! The new type may have comparison or integer/floating point division issues that the old type did not.

Note that typedefs don’t mix particularly well with Hungarian Notation, and allow you to skirt some of the issues that using Hungarian Notation tries to prevent (such as being able to change a variable’s type without having to examine the code for areas where changing the type will be problematic).

Because typedefs do not define new types, they can be intermixed like normal data types. Even though the following does not make sense conceptually, syntactically it is valid C++:

typedef long miles;
typedef long speed;

miles nDistance = 5;
speed nMhz = 3200;

// The following is okay, because nDistance and nMhz are both type long
nDistance = nMhz;

Platform independent coding

One big advantage of typedefs is that they can be used to hide platform specific details. On some platforms, an integer is 2 bytes, and on others, it is 4. Thus, using int to store more than 2 bytes of information can be potentially dangerous when writing platform independent code.

Because char, short, int, and long give no indication of their size, it is fairly common for cross-platform programs to use typedefs to define aliases that include the type’s size in bits. For example, int8 would be an 8-bit integer, int16 a 16-bit integer, and int32 a 32-bit integer. Using typedef names in this manner helps prevent mistakes and makes it more clear about what kind of assumptions have been made about the size of the variable.

In order to make sure each typedef type resolves to a type of the right size, typedefs of this kind are typically used in conjunction with the preprocessor:

#ifdef INT_2_BYTES
typedef char int8;
typedef int int16;
typedef long int32;
#else
typedef char int8;
typedef short int16;
typedef int int32;
#endif

On machines where integers are only 2 bytes, INT_2_BYTES can be #defined, and the program will be compiled with the top set of typedefs. On machines where integers are 4 bytes, leaving INT_2_BYTES undefined will cause the bottom set of typedefs to be used. In this way, int8 will resolve to a 1 byte integer, int16 will resolve to a 2 bytes integer, and int32 will resolve to a 4 byte integer using the combination of char, short, int, and long that is appropriate for the machine the program is being compiled on.

4.7 — Structs
Index
4.5 — Enumerated types

17 comments to 4.6 — Typedefs

  • billerr

    Shouldn’t the following:


    #ifdef INT_2_BYTES
    typedef int8 char;
    typedef int16 int;
    typedef int32 long;
    #else
    typedef int8 char;
    typedef int16 short;
    typedef int32 int;
    #endif

    be like this:


    #ifdef INT_2_BYTES
    typedef char int8;
    typedef int int16;
    typedef long int32;
    #else
    typedef char int8;
    typedef short int16;
    typedef int int32;
    #endif

    Aren’t int8, int16, int32 arbitrary types to use instead of char, short, int, long?

    [ Yes. The typedef declaration is defined as: typedef type-declaration synonym; so you are correct. I fixed the example. -Alex ]

  • [...] 2007 Prev/Next Posts « 4.4 — Type conversion and casting | Home | 4.6 — Typedefs » Tuesday, June 19th, 2007 at 8:24 [...]

  • [...] 4.6 — Typedefs  Print This Post This entry is filed under C++ Tutorial. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. 17 Responses to “4.7 — Structs” Comment by Skylark 2008-01-25 15:28:23 [...]

  • P,JBoy

    Is there any reason typedefs should be used rather than #defines?

    • typedefs are generally safer and harder to inadvertently screw up than #defines.

      Here’s an example of where using a typedef is better than using a #define:

      typedef int* intptr;
      intptr pX, pY; // both pX and pY are int*
      
      #define int* intptrdef;
      intptrdef pZ, pW; // pZ is int*, but pW is just int!
      

      In the second example, you were probably expecting pW to be of type int*, but it’s of type int instead!

  • CSESTUDENT

    Ok I wrote a program like this to calculate distance in km and I used DistanceCovered as alias for the type double:

     #include <iostream>
    typedef double DistanceCovered;
    
    DistanceCovered InKilomtres()
    {
        using namespace std;
        float speed;
        cout << "Please enter the speed in km/hr:" << endl;
        cin >> speed;
        float time;
        cout << "Please enter the time taken in hours:" << endl;
        cin >> time;
        return speed * time;
    }
    

    It is returning me something like this when I run it in the g++ compiler:
    /usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o: In function `_start’:
    /build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main’

    I’m sure something is wrong with my code but I can’tfind what!

    • Zak

      Well what is obviously missing is the int main() section. I took the code and copied it and added the int main() section and it worked perfectly.

      
      #include "stdafx.h"
      #include <iostream>
      
      typedef double DistanceCovered;
      
      DistanceCovered InKilomtres()
      {
          using namespace std;
          float speed;
          cout << "Please enter the speed in km/hr:" << endl;
          cin >> speed;
          float time;
          cout << "Please enter the time taken in hours:" << endl;
          cin >> time;
      
          //I added this because it was not printing the answer anywhere.
          cout << "You travelled " << speed * time << " kilometers." << endl;
          return speed * time;
      }
      
      int main()
      {
      	InKilomtres();
      	return 0;
      }
      
  • Learner

    Sorry, but I don’t really understand this part of the tutorials.
    What I’m guessing is that:

    long lVar;
    

    is the same as:

    typedef long LongChars;
    LongChars lVar;
    

    ?

  • I understand the typedef stuff ok but the :

    #ifdef INT_2_BYTES
    typedef char int8;
    typedef int int16;
    typedef long int32;
    #else
    typedef char int8;
    typedef short int16;
    typedef int int32;
    #endif
    

    This just lost me good.
    Where is the if else coming from. If “what” or else “why”. Is the preprocessor comparing something? Or is this an example of code that could be written?
    Your tutorial is great, it is filling in a lot of holes I had in my attempt to teach myself c++. My problem is that my brain has very little RAM. I guess I chewed on too many lead bars when I was kid. It was ok back then to chew on lead bars, 60 years ago you didn’t need brains like today. I was going to do a site on learning c++ but I can’t beat this site so I will put links on my sites leading to yours. This is pro stuff you got, real education taught by an educator.
    thanks

  • An alternative to using typedefs would of course be using comments, for example:

    long nDistance; // miles

    I prefer using comments for at least three reasons:

    1. You only need one line of code (if you, like me, want a linebreak after each semicolon, that is).

    2. There’s one keyword (typedef) less to learn how to use, and you can learn to use something else instead.

    3. The risk of making a mistake is greatly reduced. In other words, if you make a typo inside a comment it doesn’t matter while it usually does if you make one outside a comment.

  • I simply must comment on the smaller size and larger size javascripts, that I just discovered. I think they are really useful and that similar functions should be used much more on the web than they are.

    If you want to see an example of a similar technique, go to my image gallery Pompei with images from, not very surprisingly, Pompei.

  • SJ

    Hi,

    Will you please explain me following typedef:

    typedef WCHAR TCHAR, *PTCHAR, PTSTR;

    (Term means: WCHAR – wide character
    TCHAR – generic character
    *PTCHAR – pointer to generic character
    PTSTR – pointer to generic string. )

    Will you please explain me how i can get this typedef and how can i use this typedef.

  • Nichel

    int main()
    {
    cout <> dChoice;

    if (dChoice== ‘speed’)
    cout <<

    when i type this..it says that speed is too long ? how do i get rid of that ?!!

You must be logged in to post a comment.