C - typedef                                                                                              Home : www.sharetechnote.com

 

 

 

 

typedef

 

typedef is a keyword with which you can rename an existing name of a data type to another name that is more meaningful or convinient for you.  If you see any data type name (usually, though not all, written in all captital letters) that are used in somebody else's code that doesn't look like a default (native) data type in C, it is highly likely that those are the new name labelled to an existing (native) data type using typedef keyword.

 

 

typedef for a value =============================================

 

Example 01 >

 

#include <stdio.h>

 

typedef unsigned int POSITIVEINT;  // rename (redefine) the type 'unsigned int' into the new name POSITIVEINT

typedef unsigned char BYTE;  // rename (redefine) the type 'unsigned char' into the new name BYTE

typedef unsigned char* STRING; // rename (redefine) the type 'unsigned char*' into the new name STRING

 

void main()

{

    POSITIVEINT a = 100;

    BYTE b = 100;

    STRING str = "Hello World !";

    

    printf("Size of POSITIVEINT = %d, Value of POSITIVEINT a = %d\n",sizeof(a),a);

    printf("Size of BYTE = %d, Value of BYTE b = %d\n",sizeof(b),b);

    printf("Value of STRING str = %s\n",str);

    

}

 

Result :------------------------------------

 

Size of POSITIVEINT = 4, Value of POSITIVEINT a = 100

Size of BYTE = 1, Value of BYTE b = 100

Value of STRING str = Hello World !

 

 

typedef for struct =============================================

 

Example 01 >

 

#include <stdio.h>

 

typedef struct POINT {

    

    float x;

    float y;

    

} Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 02 >

 

#include <stdio.h>

 

struct POINT {

    

    float x;

    float y;

    

};

 

typedef struct POINT Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 03 >

 

#include <stdio.h>

 

typedef struct{

    

    float x;

    float y;

    

} POINT;

 

typedef POINT Point;

 

int main()

{

    

    Point pt;

    

    pt.x = 1.2;

    pt.y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt.x, pt.y);

    

    return 1;

}

 

 

Result :

The coordinate of pt is = (1.200000,2.100000)

 

 

Example 04 >

 

#include <stdio.h>

#include <malloc.h>

 

typedef struct POINT {

    

    float x;

    float y;

    

} Point;

 

int main()

{

    

    Point *pt;

    pt = (Point*) malloc(sizeof(Point));

    

    pt->x = 1.2;

    pt->y = 2.1;

    

    printf("The coordinate of pt is = (%f,%f)", pt->x, pt->y);

    

    return 1;

}

 

Result :

The coordinate of pt is = (1.200000,2.100000)