C typedef

C Tutorial

🔹 C typedef Tutorial

typedef in C language is used to create alias (nickname) for existing data types.
It improves code readability, portability, and maintainability.


1️⃣ What is typedef?

👉 typedef allows you to give a new name to an existing data type.

Syntax

typedef existing_datatype new_name;

2️⃣ Simple Example

Without typedef

unsigned long int number;

With typedef

typedef unsigned long int ULI;

ULI number;

✔ Code becomes shorter and cleaner


3️⃣ typedef with Basic Data Types


 

📌 INTEGER is now an alias for int


4️⃣ typedef with struct (Very Important ⭐)

❌ Without typedef


 

✅ With typedef


 

✔ No need to write struct again
✔ Very common in real projects


5️⃣ typedef with struct Name

typedef struct Student {
int id;
float marks;
} Student;

✔ Both struct Student and Student are valid


6️⃣ typedef with Pointer ⚠️ (Tricky but Important)

typedef int* INT_PTR;

int a = 10;
INT_PTR p = &a;

printf(“%d”, *p);

⚠️ Confusing Case

typedef int* PTR;
PTR p1, p2;

✔ Both p1 and p2 are pointers
❌ Not like int *p1, p2;


7️⃣ typedef with Array

typedef int ARRAY[5];

ARRAY a = {1,2,3,4,5};

✔ Useful in embedded & system programming


8️⃣ typedef with Function Pointer (Advanced 🔥)


 

📌 Used in:

  • Callbacks

  • OS kernels

  • Embedded systems


9️⃣ typedef vs #define

Featuretypedef#define
Compiler aware✅ Yes❌ No
Type checking✅ Yes❌ No
DebuggingEasyHard
Preferred✅ Yes❌ No

🔟 Common Mistakes

❌ Thinking typedef creates new type
✔ It only creates alias

❌ Pointer confusion
✔ Always use meaningful names


🔥 Real-Life Use Cases

  • OS development

  • Embedded systems

  • Device drivers

  • Large-scale C projects

  • APIs & libraries


📌 Interview Questions (Must Prepare)

  1. What is typedef in C?

  2. Difference between typedef and #define

  3. Can typedef create a new type?

  4. typedef with pointers example

  5. Why typedef is used with struct?


✅ Summary

typedef improves readability
✔ Reduces code complexity
✔ Widely used in professional C code
✔ Must-know for TCS, Wipro, Infosys, Capgemini interviews

You may also like...