typedef Keyword in C Language with Examples

The typedef keyword is a special keyword in C language. It is used to create an alternative name (alias) for an existing data type. It makes complex data type declarations simpler and improves program readability.

C लैंग्वेज में typedef एक विशेष Keyword है। इसका उपयोग किसी मौजूदा Data Type को एक वैकल्पिक नाम (Alias) प्रदान करने के लिए किया जाता है। इसकी सहायता से Complex Data Type Declarations को सरल बनाया जा सकता है तथा प्रोग्राम की Readability बढ़ाई जा सकती है।

Syntax

typedef existing_data_type new_name;

Here, existing_data_type represents an existing data type, and new_name is the alternative name assigned to that data type.

यहाँ existing_data_type किसी मौजूदा Data Type को दर्शाता है तथा new_name उस Data Type को दिया गया वैकल्पिक नाम है।

Examples of typedef Keyword

Example 1: Using typedef with Basic Data Type

#include <stdio.h>
typedef unsigned char byte;
int main(void)
{
byte a = 100;
printf("Value of a = %u\n", (unsigned int)a);
return 0;
}

Output:

Value of a = 100

Explanation: In this example, byte is created as an alternative name for unsigned char. Therefore, we can declare the variable a using byte instead of writing unsigned char.

इस उदाहरण में byte को unsigned char का वैकल्पिक नाम बनाया गया है। इसलिए हम unsigned char लिखने के स्थान पर byte का उपयोग करके वेरिएबल a डिक्लेअर कर सकते हैं।

Example 2: Using typedef with Structure

#include <stdio.h>
struct student
{
int rollno;
char name[20];
float fees;
};
typedef struct student stu;
int main(void)
{
stu s1 = {101, "Amit", 15000.0f};
stu s2 = {102, "Rahul", 18000.0f};
printf("Student 1\n");
printf("Roll No = %d\n", s1.rollno);
printf("Name = %s\n", s1.name);
printf("Fees = %.2f\n", s1.fees);
printf("\nStudent 2\n");
printf("Roll No = %d\n", s2.rollno);
printf("Name = %s\n", s2.name);
printf("Fees = %.2f\n", s2.fees);
return 0;
}

Output:

Student 1
Roll No = 101
Name = Amit
Fees = 15000.00
Student 2
Roll No = 102
Name = Rahul
Fees = 18000.00

Explanation: In this example, stu is created as an alternative name for struct student. Therefore, we can declare structure variables using stu instead of writing struct student every time.

इस उदाहरण में stu को struct student का वैकल्पिक नाम बनाया गया है। इसलिए प्रत्येक बार struct student लिखने के स्थान पर हम stu का उपयोग करके Structure Variables डिक्लेअर कर सकते हैं।

Example 3: Using typedef Directly with Structure

We can also use typedef while defining a structure. This allows us to create the structure and its alternative name in a single declaration.

हम Structure की Definition के साथ सीधे typedef का उपयोग भी कर सकते हैं। इससे Structure और उसका वैकल्पिक नाम एक ही Declaration में बनाया जा सकता है।

#include <stdio.h>
typedef struct
{
int rollno;
char name[20];
float fees;
} stu;
int main(void)
{
stu s1 = {101, "Amit", 15000.0f};
printf("Roll No = %d\n", s1.rollno);
printf("Name = %s\n", s1.name);
printf("Fees = %.2f\n", s1.fees);
return 0;
}

Output:

Roll No = 101
Name = Amit
Fees = 15000.00

Explanation: Here, stu is the alternative name of the anonymous structure. We can directly declare structure variables using stu.

यहाँ stu, Anonymous Structure का वैकल्पिक नाम है। हम सीधे stu का उपयोग करके Structure Variables डिक्लेअर कर सकते हैं।

Example 4: Using typedef with an Array

The typedef keyword can also be used to create an alternative name for an array type.

typedef Keyword का उपयोग Array Type को वैकल्पिक नाम देने के लिए भी किया जा सकता है।

#include <stdio.h>
typedef int marks[5];
int main(void)
{
marks m = {70, 80, 65, 90, 75};
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", m[i]);
}
return 0;
}

Output:

70 80 65 90 75

Explanation: In this example, marks is an alternative name for an array of five integers. Therefore, marks m declares an integer array containing five elements.

इस उदाहरण में marks, पाँच Integer Elements वाले Array का वैकल्पिक नाम है। इसलिए marks m लिखने पर पाँच Elements वाला Integer Array डिक्लेअर होता है।

Important Points About typedef

  1. typedef is a keyword in C language.

    typedef C लैंग्वेज का एक Keyword है।

  2. It creates an alternative name for an existing data type.

    यह किसी मौजूदा Data Type का वैकल्पिक नाम बनाता है।

  3. It does not create a completely new data type.

    यह पूरी तरह से नया Data Type नहीं बनाता है।

  4. It can be used with basic data types, structures, unions, pointers and arrays.

    इसका उपयोग Basic Data Types, Structures, Unions, Pointers और Arrays के साथ किया जा सकता है।

  5. It makes complex declarations easier to read and understand.

    यह Complex Declarations को पढ़ना और समझना आसान बनाता है।

The typedef keyword is useful for simplifying data type declarations in C language. It is especially helpful when working with structures, unions, pointers and complex data types.

C लैंग्वेज में typedef Keyword Data Type Declarations को सरल बनाने के लिए उपयोगी है। यह विशेष रूप से Structures, Unions, Pointers और Complex Data Types के साथ कार्य करते समय अधिक सहायक होता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025