Making a type definition

It is very often convenient to associate an alias with a type, so that the programmer can subsequently declare variables to be of this named type. If the type used for these variables needs to be changed, for instance to use a short integer to save space, only the aliasing definition needs to be changed.

Type definitions can be made outwith or within functions. Scope is as for variables.

    enum Rainbow {red,orange,yellow,green,blue,indigo,violet};

	typedef enum Rainbow Spectrum; /*Alias Spectrum to Rainbow */
	typedef int WholeNum;          /* Alias WholeNum to mean int */

	Spectrum S1, S2;               /*Variables S1 and S2 of type Spectrum */
	WholeNum D1, D2;               /*Variables D1 and D2 of type WholeNum */

Exercises on this section.


Next - Array definition.

Back to Contents page.