use of typedef in c programming Language.

2. type def with struct.

In c programming some times we need our own data type to declare variable of different data types as per the requirement of the problem. In C programming language this can be achieved using the keywords typedef and struct.
With the help of these keywords we can group non-homogeneous data types into a single group.
The use of type def with structures increase the code readability and we don’t have to type struct repeatedly.
The user can create new data type using type def struct.
The typedef keyword provides meaningful names to existing data type in C and makes it more understandable.


Consider the following example for type def.
eg.1.

struct employee
 {
  char name[20];
  int salary;
 };
 struct employee e1;

In the above given structure declaration, we have created the variable e1 of employee type by writing the following statement:
struct employee e1;
The above statement is the creation of a structure variable, i.e e1,but the statement and process is quite big.
typedef keyword is used to avoid such a big statement.


eg.2

typedef struct employee
 {
  int empno;
  char name[20];
  float salary;
 } emp1;
 emp1 e1,e2;

The given above declaration is very easy to understand and use typedef keyword reduces the length of the code and complexity of data types.


C program to implement typedef with structure in C.


#include<stdio.h>
#include<string.h>
// using typedef to define an alias for structure
  typedef struct employee
 {
  int empno;
  char name[20];
  float salary;
 } emp1;

int main()
 {
  emp1 e1;
 e1.empno=7900;
 strcpy(e1.name, "Warade K.");
 e1.salary=45600.98;
 printf("Employee Number=%d",e1.empno);
 printf("Name=%s",e1.name);  printf("Salary=%.2f",e1.salary);
  return (0);
}

Output:
  Employee Number=7900
  Name=Warade K.
  Salary=45600.90


type def with pointer.

The typedef keyword in C programming is used to create another name for the variable.It may be used to declare a number of pointers of the same type.typedef does not change the existing type but just creates a synonym , synonyms means another or alternate name for the same type.
Let understand from the following example.

typedef int*  Int_ptr;  Int_ptr represents int*
Int_ptr   var1;
Int_ptr  var2;
Int_ptr  var3;
int x,y,z;
var1=&x;
var2=&y;
var3=&z;   // Use of Int_ptr

In the above given code the integer pointer var1,var2,var3 are initialized with addresses of x, y, and z respectively.

C program to Demonstrate use of typedef in pointer declaration.
#include <stdio.h>
int main()
{
 typedef float* FP;
  float x1 = 10.15,y1 = 8.16, z1 = 12.15;
  FP px = &x1 , py = &y1, pz =&z1;
  printf("x1 = %.3f,\t *px = %.3f\t px = %p \n", x, *px, px);
  printf("y1 = %.3f,\t *py = %.3f\t py = %p \n", y, *py, py);
  printf("z1 = %.3f,\t *pz = %.3f\t pz = %p \n", z, *pz, pz);
return(0);
}

  The expected output is as given below.
&ems;px1=10.150 *px=10.150 px=FFEC
 y1=8.160 *py=8.160& py=FFE4
 z1=12.150 *pz=12.150 pz=FFDC
FP is a new alias with the pointer type float *. The definition FP px;, defines a variable px with the type float *. So, px is a pointer which can point to a variable of type float.Simillarly py and pz points to the variable type float.

Previous Topic:-->> Declare Structure Variable || Next topic:-->>Access Structure Member