# Declaring New Data Types with typedef

* ***typedef*** is a powerful tool that allows programmers to define and then use their own data types. For example:
    
    `typedef int Integer;`
    
    `Integer nStudents, nCourses, studentID;`
    
* Note that in the typedef statement, the newly defined type name goes in the place where a variable name would normally go.
    
* There are a few benefits of using typedef with simple types such as the example above:
    
    * For readability, "Integer" may be easier to understand than "int".
        
    * The typedef can be easily changed later, ( say to `typedef long int Integer;` ), which would then affect all variables of the defined type.
        

```c
#include <stdio.h>

typedef long long int Integer;

int main(void) {
    Integer n;
    printf("Enter the value of n:");
    scanf("%lld",&n);
    printf("\nThe number is: %lld",n);
    
    return 0;
}
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711379446944/928fc839-eb07-4f29-ad23-506be2002d05.png align="center")

The real benefit of typedef comes into play with complex data structures, such as structs. Say we have a Point structure:

```c
struct Point
{
    int x;
    int y;
} A;

int main(){
    printf("Enter the x-coordinate");
    scanf("%d",&A.x);
    printf("Enter the y-coordinate");
    scanf("%d",&A.y);
    printf("\nThe point A is (%d,%d).",A.x,A.y);

    struct Point B;

    printf("\nEnter the x-coordinate");
    scanf("%d",&B.x);
    printf("Enter the y-coordinate");
    scanf("%d",&B.y);
    printf("\nThe point B is (%d,%d).",B.x,B.y);
}
```

With typedef :

```c
typedef struct Point
{
    int x;
    int y;
} Location;

int main(){
    Location A;

    printf("Enter the x-coordinate");
    scanf("%d",&A.x);
    printf("Enter the y-coordinate");
    scanf("%d",&A.y);
    printf("\nThe point A is (%d,%d).",A.x,A.y);

    Location B;

    printf("\nEnter the x-coordinate");
    scanf("%d",&B.x);
    printf("Enter the y-coordinate");
    scanf("%d",&B.y);
    printf("\nThe point B is (%d,%d).",B.x,B.y);
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711379741760/004493d1-1807-42b7-993b-7f7d4651d17b.png align="center")

Even this is allowed:

```c
typedef struct Point
{
    int x;
    int y;
} Point;

int main(){
    Point A;

    printf("Enter the x-coordinate");
    scanf("%d",&A.x);
    printf("Enter the y-coordinate");
    scanf("%d",&A.y);
    printf("\nThe point A is (%d,%d).",A.x,A.y);

    Point B;

    printf("\nEnter the x-coordinate");
    scanf("%d",&B.x);
    printf("Enter the y-coordinate");
    scanf("%d",&B.y);
    printf("\nThe point B is (%d,%d).",B.x,B.y);
}
```

typedef saves some typing and makes the code clearer to read.
