What is Typedef in C Programming Language with Example

What is Typedef in C Programming Language with Examples

A Typedef in C in various keywords and data types are 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.

You must test the C Language code provided in this lesson in your code editor to validate it. However, if you prefer to execute this code online, we also provide a free C Online Compiler for testing your C Language code.

What is the typedef keyword 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.

Working of C typedef

How to Run C in Visual Studio Code ...
How to Run C in Visual Studio Code on Windows 10 2022 Best Code Editor

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;

Your user-defined data types can also be given names using typedef. For instance, you can construct a new data type using typedef with structure and then use that data type to define structure variables directly as seen below.

#include <stdio.h>
#include <string.h>
 
typedef struct Books {
   char books_title[100];
   char books_author[100];
   char books_subject[100];
   int book_id;
} Book;
 
int main( ) {
   Book book;
 
   strcpy( book.books_title, "Typedef in C Programming");
   strcpy( book.books_author, "itsourcecode"); 
   strcpy( book.books_subject, "C Programming Tutorial for Beginners");
   book.book_id = 51820110;
 
   printf( "Book title : %s\n", book.books_title);
   printf( "Book author : %s\n", book.books_author);
   printf( "Book subject : %s\n", book.books_subject);
   printf( "Book book_id : %d\n", book.book_id);
   return 0;
}

Output of the above example!

Book title : Typedef in C Programming
Book author : itsourcecode
Book subject : C Programming Tutorial for Beginners
Book book_id : 51820110

You can try to test here the example above!

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.

Basic Example of C typedef keyword

#include<stdio.h>
typedef int ainT; //declared a new variable of int data type!
int main ()
{
  printf("C Programming Tutorial: Example of typedef keyword!\n\n");
  int x=69;
  ainT y=49,z;
  z = x + y;
  printf ("Sum is: %d", z);
  return 0;
}

Output of the above example!

C Programming Tutorial: Example of typedef keyword!

Sum is: 118

You can try to test here the example above!

Example of typedef in C

By designing a structure, you may construct a record of a student that contains information about the student, such as name, ID, subjects, marks, phone number, etc. You can use it to make the computer code easier to read.

#include<stdio.h>
typedef struct details
{
int stud_age;
int stude_id;
}student;
int main( )
{
student student1;
printf("C Programming Tutorial: Example of typedef in C!\n\n");
student1.stud_age = 21;
student1.stude_id = 3;
printf("AGe of the student is: %d\n", student1.stud_age);
printf("Id of the student is: %d\n", student1.stude_id);
return 0;
}

Output of the above example!

C Programming Tutorial: Example of typedef in C!
Age of the student is: 21
Id of the student is: 3

You can try to test here the example above!

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;

Example of typedef with structures

#include <stdio.h>
typedef struct employee
{  
int emp_sal;  
int emp_id;  
}emp;  
int main()  
{  
emp employee1,employee2;  
employee1.emp_sal = 14000;
employee1.emp_id = 1;
employee2.emp_sal = 12000;
employee2.emp_id = 2;
printf("C programming Tutorial: typedef using structures in C Language!");
printf("Salary of the first employee is : %d\n", employee1.emp_sal);
printf("ID of the first employee is : %d\n", employee1.emp_id);
printf("Salary of the second employee is : %d\n", employee2.emp_sal);
printf("ID of the second employee is : %d\n", employee2.emp_id);
return 0;  
}

Output of the above example!

C programming Tutorial: typedef using structures in C Language!Salary of the first employee is : 14000
ID of the first employee is : 1
Salary of the second employee is : 12000
ID of the second employee is : 2

In the above example, we have declared variable emp of type struct employee. We can create variables using the emp variable of type struct employee. It helps in reducing the complexity of the code.

You can try to test here the example above!

How to use typedef 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.

C Typedef with Pointers

The typedef keyword is also compatible with pointers.

Example using typedef with pointers

#include <stdio.h>
typedef int point;
int main(){
  point x1= 40;
  x1 = 40;
  printf("C Programming Tutorial: typedef using pointers!\n");
  printf("Value is: %d",x1);
  return 0;
}

Output of the above example!

C Programming Tutorial: typedef using pointers!
Value is: 40

You can try to test here the example above!

Typedef and #define

The typedef keyword and the #define command both function similarly. Similar to the typedef keyword, the #define directive is used to specify different data types.

The preprocessor processes the #define directive, and the compiler processes the typedef keyword.

A typedef keyword requires a semicolon at the end. But you don’t have to in #define.

The typedef function is used to rename an existing data type. Additionally, the #define directive is used to give an alias for values such as 1 as ONE and 3.14 as PI.

Example of #define

#include <stdio.h>
#define SIDE  5
int main( ) {
  int a;
  a = SIDE*SIDE;
   printf("C Programming Tutorial: #define directive!\n");
   printf("Area of square is: %d",a);
   return 0;
}

Example of typedef in C

#include <stdio.h>
typedef char x;
int main()
{
  x y1;
  y1 = 'C Programming Typedef';
  printf("Printing character: %c", y1);
  return 0;
}

Examples of how to use the typedef struct are provided below.

Example of Variable declaration without using typedef:

#include <stdio.h>
typedef char x;
int main()
{
  x y1;
  y1 = 'C';
  printf("Printing character: %c", y1);
  return 0;
}

Output of the above example!

Printing character: C

You can try to test here the example above!

Examples of how to use the typedef struct are provided below.

Example of Variable declaration without using typedef:

#include<stdio.h>
struct value{
  int x;
};
int main() {
  struct value y;
  y.x=69;
  printf("Value is: %d\n", y.x);
  return 0;
}

Output of the above example

Value is: 69

You can try to test here the example above!

Difference between typedef and #define:

typedef#define
It is compiler 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.

Application of typedef

The existing data type is given a meaningful name using the typedef keyword.

Use of typedef with structures:

typedef struct
{
data_type var1;
data_type var2;
}variable_name;

Example typedef unsigned char

#include <stdio.h>
typedef unsigned char CHAR;
int main()
{
CHAR number1='I';
CHAR number2='T';
CHAR number3='S';
CHAR number4='O';
CHAR number5='U';
CHAR number6='R';
CHAR number7='C';
CHAR number8='E';
CHAR number9='C';
CHAR number10='O';
CHAR number11='D';
CHAR number12='E';
printf("C Programming Tutorial: typedef with unsigned char!\n\n");
printf("Created word with alphabets: %c%c%c%c%c%c%c%c%c%c%c%c",number1,number2,number3,number4,number5,number6,number7,number8,number9,number10,number11,number12);
return 0;
}

Output of the above example!

C Programming Tutorial: typedef with unsigned char!

Created word with alphabets: ITSOURCECODE

You can try to test here the example above!

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 try to test here the example above!

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.

Your 15 seconds will motivate us to put forth even more effort.

Inquiries

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 next following post, I’ll create a input and output in C and try to explain its many components in full details. 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