C typedef

C is such a dominant language of its time and now, that even you can name those primary data type of your own and can create your own named data type by blending data type and its qualifier.

The typedef keyword in C

typedef is a C keyword implemented to tell the compiler for assigning an alternative name to C's already exist data types. This keyword, typedef typically employed in association with user-defined data types in cases if the names of datatypes turn out to be a little complicated or intricate for a programmer to get or to use within programs. The typical format for implementing this typedef keyword is:

typedef <existing_names_of_datatype> <alias__userGiven_name>;

Here's a sample code snippet as of how typedef command works in C:

typedef signed long slong;

slong in the statement as mentioned above is used for a defining a signed qualified long kind of data type. Now the thing is this 'slong', which is an user-defined identifier can be implemented in your program for defining any signed long variable type within your C program. This means:

slong g, d;

will allow you to create two variables name 'g' and 'd' which will be of type signed long and this quality of signed long is getting detected from the slong (typedef), which already defined the meaning of slong in your program.

Various Application of typedef

The concept of typedef can be implemented for defining a user-defined data type with a specific name and type. This typedef can also be used with structures of C language. Here how it looks like:

typedef struct
    type first_member;
    type sec_member;
    type thrid_member;
} nameOfType;

Here nameOfType correspond to the definition of structure allied with it. Now, this nameOfType can be implemented by declaring a variable of this structure type.

nameOfType type1, type2;

Simple Program of structure in C with the use of typedef:


typedef struct professor
    char p_name[50];
    int p_sal;
} prof;

void main(void)
    prof pf;
    printf("\n Enter Professor details: \n  \n");
    printf("\n Enter Professor name:\t");
    scanf("% s", pf.p_name);
    printf("\n Enter professor salary: \t");
    scanf("% d", &pf.p_sal);
    printf("\n Input done ! ");

Using typedef with Pointers

typedef can be implemented for providing a pseudo name to pointer variables as well. In this below-mentioned code snippet, you have to use the typedef, as it is advantageous for declaring pointers.

int* a;

The binding of pointer (*) is done to the right here. With this kind of statement declaration, you are in fact declaring an as a pointer of type int (integer).

typedef int* pntr;
pntr g, h, i;