🟡 Intermediate  ·  Lesson 32

typedef in C

typedef

What is typedef?

typedef is used to create a new name or alias for an existing data type. It improves readability, especially with structures and pointers.

Syntax
typedef existing_type new_name;

Basic typedef

C Language
#include <stdio.h>

typedef unsigned int uint;
typedef long long ll;

int main() {
    uint age = 25;
    ll population = 1400000000LL;

    printf("Age = %u\n", age);
    printf("Population = %lld", population);
    return 0;
}

typedef with struct

Without typedef, we must write struct Student each time. With typedef, we can write only Student.

C Language
#include <stdio.h>

typedef struct {
    int roll;
    char name[30];
    float marks;
} Student;

int main() {
    Student s1 = {101, "Aman", 88.5};
    printf("%d %s %.1f", s1.roll, s1.name, s1.marks);
    return 0;
}
101 Aman 88.5

typedef with Pointer

C Language
typedef int* IntPtr;

int main() {
    int x = 10;
    IntPtr p = &x;
    printf("%d", *p);
    return 0;
}
⚠️ Pointer typedef caution

IntPtr a, b; makes both a and b pointer variables, but normal int *a, b; makes only a a pointer.

Summary

  • typedef creates an alias for existing type
  • It does not create a new data type internally
  • Very useful with structures
  • Improves readability in large programs
  • Use clear names like Student, uint, Node

typedef क्या है?

typedef existing data type का नया नाम या alias बनाने के लिए use होता है। इससे code readable बनता है।

Syntax
typedef existing_type new_name;

Basic typedef

C Language
typedef unsigned int uint;
typedef long long ll;

uint age = 25;
ll population = 1400000000LL;

struct के साथ typedef

C Language
typedef struct {
    int roll;
    char name[30];
    float marks;
} Student;

Student s1 = {101, "Aman", 88.5};

Pointer के साथ typedef

C Language
typedef int* IntPtr;
int x = 10;
IntPtr p = &x;
⚠️ ध्यान दें

Pointer typedef का नाम clear रखें ताकि confusion न हो।

सारांश

  • typedef existing type का alias बनाता है
  • Internal नया type नहीं बनता
  • Structures के साथ बहुत useful है
  • Code clean और readable बनता है
  • Clear names use करें जैसे Student, Node, uint
← Back to C Tutorial