C Typedef

C Typedef

The typedef keyword in C is used to give existing data types a new name. It’s essentially a way to redefine the names of existing data types. This is especially useful when the original names of data types are cumbersome to work with in your programs. typedef can also be used to create aliases for user-defined data types, making them more user-friendly and easier to work with in your code.

C typedef Syntax

typedef existing_name alias_name;

Therefore, after this declaration, we can use  alias_name just like the real existing_name in our C program.

Example of typedef in C

typedef long long ll;

C program to illustrate typedef


// C program to implement typedef
#include <stdio.h>
 
// defining an alias using typedef
typedef long long ll;
 
// Driver code
int main()
{
    // using typedef name to declare variable
    ll var = 20;
    printf("%ld", var);
 
    return 0;
}

Output

20

Use of typedef in C

  1. Meaningful Names: It provides a meaningful name for an existing data type, making the code more understandable for other users.
  2. Structures: It can be used with structures, eliminating the need to repeatedly type “struct” and improving code readability.
  3. Pointers: typedef can simplify the declaration of multiple pointers in a single statement, reducing redundancy.
  4. Arrays: It allows you to declare multiple variables with ease when used with arrays.

In summary, typedef enhances code readability and manageability by giving clear, user-friendly names to data types and simplifying the declaration of complex data structures like structures, pointers, and arrays

1. typedef struct

Typedef can even be used with structures in the C programming language. A new data type can be produced and used to define the structure variable.

Example 1: Using typedef to define a name for a structure


// C program to implement
// typedef with structures
#include <stdio.h>
#include <string.h>
 
// using typedef to define an alias for structure
typedef struct students {
    char name[50];
    char branch[50];
    int ID_no;
} stu;
 
// Driver code
int main()
{
    stu st;
    strcpy(st.name, "Kamlesh Joshi");
    strcpy(st.branch, "Computer Science And Engineering");
    st.ID_no = 108;
 
    printf("Name: %s\n", st.name);
    printf("Branch: %s\n", st.branch);
    printf("ID_no: %d\n", st.ID_no);
    return 0;
}

Output

Name:Kamlesh Joshi
Branch:Computer Science And Engineering
ID_no : 108;

2. typedef with Pointers

The typedef keyword is indeed very efficient when used with pointers, as it allows you to provide alias names for pointer types. This can significantly enhance code readability and simplify the declaration of multiple pointers in a single statement. When using typedef with pointers, the pointers are bound to the right of the simple declaration, making the code more concise and easier to understand.

Example

typedef int* Int_ptr;
Int_ptr var, var1, var2;

Example 2: Using typedef to define a name for pointer type



// C program to implement
// typedef with pointers
#include <stdio.h>
 
typedef int* ptr;
 
// Driver code
int main()
{
    ptr var;
    *var = 20;
 
    printf("Value of var is %d", *var);
    return 0;
}

Output

Value of var is 20

3. typedef with Array

typedef can be used with array in order to increase their count.

Example

typedef int arr[20]

arr is basically an alias for the array of 20 elements.

// it's same as Arr[20], two-Arr[20][23];
arr Arr, two-Arr[23];

Example 3: Using typedef to define an alias for

Array



// C program to implement typedef with array
#include <stdio.h>
 
typedef int Arr[4];
 
// Driver code
int main()
{
    Arr temp = { 10, 20, 30, 40 };
    printf("typedef using an array\n");
 
    for (int i = 0; i < 4; i++) {
        printf("%d ", temp[i]);
    }
    return 0;
}

Output

typedef using an array
10 20 30 40

C typedef vs #define

The differences between #define and typedef in C is given below

  1. Alias Definitions: #define can define aliases for values, like mapping 1 to ONE or 3.14 to PI, whereas typedef is used to give symbolic names to types only.
  2. Processing Stage: Preprocessors interpret #define statements, while the compiler interprets typedef statements. #define is processed before the compilation stage.
  3. Semicolons: There should be no semicolon at the end of a #define statement, but there should be a semicolon at the end of a typedef statement.
  4. Type Creation: typedef defines a new type by copying and pasting the definition values, making it more suitable for creating new data types, while #define simply performs text substitution, which is more suitable for constants and macros.

C program to implement #define


// C program to implement #define
#include <stdio.h>
 
// macro definition
#define LIMIT 3
 
// Driver code
int main()
{
    for (int i = 0; i < LIMIT; i++) {
        printf("%d \n", i);
    }
    return 0;
}

Output

0 
1 
2 

FAQ- C Typedef

Q1. What is an example of a typedef?

Ans. For example: typedef class { Trees(); } Trees; Here the function Trees() is an ordinary member function of a class whose type name is unspecified. In the above example, Trees is an alias for the unnamed class, not the class type name itself, so Trees() cannot be a constructor for that class.

Q2. What is typedef syntax?

Ans. Typedef Keyword: The typedef keyword is used to indicate that you’re defining an alias for an existing data type.
Existing Data Type: This is the name of the existing data type or variable that you want to create an alias for.
Alias Name: This is the new name you’re assigning to the existing data type, making it easier to use and understand in your code.

Q3. Can we use typedef in C?

Ans. It allows users to define alternative names for both primitive (e.g., int) and user-defined (e.g., struct) data types. Importantly, it’s crucial to understand that typedef does not create new data types; instead, it provides convenient aliases or alternative names for existing data types. This feature enhances code readability and simplifies data type usage in C programming.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment