TYPEDEF (TYPE DEFINITION) - C-Tutorial

Latest

Thursday, 20 October 2016

TYPEDEF (TYPE DEFINITION)

TYPEDEF (TYPE DEFINITION)


            The typedef keyword allows the user to specify a new name for the existing data types.  The general format of the declaration statement using the typedef keyword is:
           
            Syntax                        :           typedef ExistingDataType NewName;

Where,
     ExistingDataType may be either a primitive data type of user-defined data type.

Note:   typedef declaration does not create any new data types.  It just adds a new name for the existing data type.

            Example:         1.         typedef int Integer;
                                                Integer x,y,z;

                                    2.         typedef char Gender;
                                                Gender x=’F’, y=’M’;


Ø  typedef keyword is very useful in the case of user-defined data types like structure.  While using typedef keyword with the structure creation, structure tag is optional.

Example:         typedef struct
                        {
                                    float real, imag;
                        }Complex;

                        Complex k;


/* EXAMPLE PROGRAM OF TYPEDEF WITH STRUCTURE */

#include<stdio.h>
#include<conio.h>

typedef struct
{
            float  real, imag;
}Complex;

main()
{
            Complex k;
            clrscr();
            printf("\nEnter Real Part:");
            scanf("%f",&k.real);
            printf("\nImaginary Part:");
            scanf("%f",&k.imag);
            printf("\nComplex Number is =%.2f + i %.2f",k.real,k.imag);

}

No comments:

Post a Comment