by Dinesh Thakur Category: Type And Variables

The typedef feature allows us to give an alternative (possibly short and more meaningful) name to an existing data type and improve program readability. For example, instead of using the int data type to declare variables to represent marks in three subjects, we can associate a more meaningful name (say Marks)for the int data type using typedef as:

typedef int Marks;

Now we can declare variables using the new type name Marks as shown below:

Marks phy, chem, math;

It should be understood that Marks is just another name for the int data type and it does not restrict the values of variables phy,chem and math to the desired range, say 0 to 100. Also, we can perform all the operations on these variables that we can perform on variables of type int including the use of the %d format in the scanf and printf statements.

The typedef can be used to give convenient short names to buit-in data types, as shown below.

typedef unsigned short int UShort;

typedef long double LDouble;

typedef const unsigned long int ULongConst;

These typedefs declare ushort, LDouble and ULongConst as convenient shorthands for unsigned short int, long double and const unsigned long int, respectively. Note that while declaring variables using type name ULongConst, we must initialize them; otherwise, the compiler will report an error. Also note that we have used capitalized names for typedef names as a convention.

We can use other features of the C language in conjunction with the new type names. For example, we can define const variables, arrays and pointers using type name UShort as

const UShort a = 1234;

UShort b[10];

UShort *pa = &a;

The typedef is a powerful mechanism that allows us to declare new names for complex types that involve arrays, pointers, functions and their combinations. The syntax for such declarations can be quite confusing in the beginning. However, it is not very difficult if we use the following rule: To declare a new type name for a particular type, first declare this name as if we were declaring a variable of that type and then precede that declaration with the typedef keyword.

For example, to declare UShort as a new type name for unsigned short int, first declare UShort; as if it is a variable of that type as

unsigned short int UShort;

and then precede this declaration with the typedef keyword as shown below.

typedef unsigned short int UShort;

Finally note that the typedefs are generally written at the beginning of a program, usually after the #include statements. This allows their use in any of the structures, enums and functions including the main function. We can also write them in any function, in which case, their use is restricted to that function only.

Using typedef with Structures

Recall that we have to use the struct keyword every time we declare a structure variable or parameter. This inconvenience can be avoided using typedef. For example, consider the typedef given below.

typedef struct complex {

float re, im;

) Complex;

This code declares Complex as a new type name for struct complex. Note that as per our convention, the type name is in capitalized case (complex),whereas the structure name (complex) is in lowercase letters. Also, observe how our rule simplifies writing such declarations: the new type name Complex is first written as if it is a variable of type struct complex and then the declaration is preceded by the typedef keyword.

If you prefer, the declarations of structure and typedef can be written separately as

struct complex {

float re, im;

} ;

typedef struct complex Complex;

Observe that our rule for writing a typedef is applicable in this case as well.

Once the new type name Complex is declared using either of the approaches given above, we can declare variables using this new type name (as well as by using struct complex)as shown below.

Complex a = {1,2};

struct complex b, c;

Now we can simplify the definition of the complex_add function using this new type name and greatly improve its readability, as shown below.

/* addition of two complex numbers */

Complex complex_add(Complex a, Complex b)

{

  Complex c;

  c.re = a.re + b.re;

  c.im = a.im + b.im;

  return c;

}

Note that as discussed earlier, we can continue to use other features of the C language in conjunction with a new type name. For example, we can declare arrays and pointers of this type as

Complex a[10];

Complex *pa;

Finally note that the typedef name can be the same as that of the structure name. For example,

typedef struct Complex {

float re, im;

} Complex;

Also, we can even omit the structure name as shown below.

typedef struct {

float re, im;

) Complex;

Using typedef with Enumerated Types

Thus, we can declare a new type name for enumerated types using typedef as

typedef enum color {Violet, Indigo, Blue, Green, Yellow, Orange, Red} RbColor;

or if you prefer, we can separate these declarations as

enum color {Violet, Indigo, Blue, Green, Yellow, Orange, Red};

typedef enum color RbColor;

These declarations define RbColor as a new type name for enumcolor. We can use this new type name wherever we can use enumcolor. Thus, we can define various entities using this new type name, such as variables, arrays, pointers, function parameters, structure members, etc. In addition, we can also use enumcolor for this purpose, although it is unnecessary now. For example, we can declare variables as shown below.

RbColor clr = Blue;

enum Color cl = Violet, c2 = Red;

Note that the typedef name can be the same as that of the enumerated type name and we can also omit the enumerated type name, as in case of typedefs for structures.

Illustrates application of typedef.

#include <stdio.h>

int main()
{
   typedef int I;
   typedef double dbl;
   I a =2,b =6;
   dbl K, S, T = 3.0;
   clrscr();
   K = b/T;
   S = T/ a;
   printf(" K = %f\t s = %f\n", K,S);
   return 0;
}

typedef in c





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.