Typedef in C Language with Best Example

A C typedef in various keywords and data types is supported by the C programming language.

Additionally, you can create your own data type in C. Typedef is a keyword that is predefined.

This keyword facilitates the creation of a user-defined data type name.

What is typedef in C?

As previously mentioned, it is a predefined keyword.

Using this keyword, you can give an existing variable in your C program a meaningful name.

You can, in short, replace the existing data type’s name with the one you’ve provided.

It operates similarly when command aliases are defined.

What does typedef do in c?

To successfully implement the typedef keyword, you must give an existing data type a meaningful name in the program code.

The compiler then replaces the existing data type with the name you specify for the entire program.

The C programming language provides the typedef keyword, which can be used to rename a type.

The example that follows defines the term BYTE for one-byte numbers.

typedef unsigned char BYTE;

After this type definition, the identifier BYTE may be used, for instance, as an abbreviation for the type unsigned char.

BYTE b1, b2;

Uppercase letters are typically used in these definitions to remind the user that the type name is actually a symbolic abbreviation, although lowercase letters may also be used as seen below.

typedef unsigned char byte;

Syntax Using typedef keyword in C

typedef <existing data type or keyword> <user given name for the datatype or keyword>

Syntax Working of typedef

typedef struct
{
    data_type mem1;
    data_type mem2;
    data_type mem3;
}type_name;

Example of typedef keyword in C

typedef unsigned int length;

The typedef keyword was used in the example above to declare an unsigned int type variable length. Now, unsigned int type variables of the following length can be created.

length x,y;

Let’s say you need to define an unsigned int datatype multiple times throughout your program’s code.

The job will be slack. However, the typedef keyword will simplify your task.

Why typedef struct in c?

Typedef struct in c the typedef keyword can also be used with structures.

Create a new data type with typedef, then define structural variables using that type.

Example Using typedef with structures

typedef struct employee
{  
int emp_salary;  
int emp_id;  
}emp;
emp emp_1,emp_2;

How does typedef work in c?

You can use the typedef keyword in C.

It is mostly used to give existing data types meaningful names.

A real int variable has been declared below using the typedef keyword.

Difference between typedef and #define:

typedef#define
It is preprocessor-based.It is preprocessor based.
You have to put a semicolon at the end of the typedef keyword.In #define directive, you don’t have to put a semicolon.
It is used for giving new names to data types only.#define is a directive which is used to give an alias for values like 1 as ONE, 3.14 as PI.
Describes the actual definition of the new data type.Simply copy and paste the definition values.

C Typedef using an array

The typedef keyword can also be used with an array.

For Example:

typedef int arr[10];

arr is an int data type variable. With it, you can specify as many variables as you like.

For example:

arr a,b,c;

Example of typedef with an array

#include<stdio.h>
typedef int arr[5];
int main()
{
  int x;
  arr y = {14,2,3,54,3};
  printf("C Programming Tutorial: typedef using array!\n");
  for(x = 0; x < 5; x++)
  {
      printf("Element of %d is: %d\n",x ,y[x]);
  }
  return 0;
}

Output of the above example:

C Programming Tutorial: typedef using array!
Element of 0 is: 14
Element of 1 is: 2
Element of 2 is: 3
Element of 3 is: 54
Element of 4 is: 3

You can test the above example here! ➡ C Online Compiler

Summary

A predefined keyword is typedef. The name you have provided can be used in place of the name of the current data type.

This term aids in giving an existing data type a user-defined name. The typedef keyword can also be used with arrays, pointers, structures, etc.

If you have any questions or suggestions about What is typedef in C language with example?, please feel free to leave a comment below.

About Next Tutorial

In the following post, I’ll create an input and output in C and try to explain its many components in full detail.

I hope you enjoy this post on What is Typedef in C Programming Language with Examples, in which I attempt to illustrate the language’s core grammar.


Leave a Comment