The typedef keyword lets you create a new name for an existing type. This makes your code easier to read and write.
Typedef keyword in C
typedef existing_type new_type_name;
The existing_type can be any valid C type like int, struct, or pointer types.
The new_type_name becomes an alias you can use instead of the original type.
uint for unsigned int. Now you can use uint instead of unsigned int.typedef unsigned int uint; uint age = 30;
Point for it. You can declare variables of type Point easily.typedef struct {
int x;
int y;
} Point;
Point p1 = {10, 20};String for char*, making pointer usage clearer.typedef char* String; String name = "Alice";
This program uses typedef to create a new type Student for a struct. Then it creates a variable s1 of type Student and prints its fields.
#include <stdio.h>
typedef struct {
int id;
char name[20];
} Student;
int main() {
Student s1 = {101, "John"};
printf("ID: %d, Name: %s\n", s1.id, s1.name);
return 0;
}typedef does not create a new type, it creates a new name for an existing type.
You can use typedef with pointers, arrays, structs, and basic types.
Using typedef can make your code cleaner and easier to maintain.
typedef creates a new name for an existing type.
It helps simplify complex type names and improve code readability.
Commonly used with structs and pointers to make code easier to write and understand.
Practice
typedef keyword in C?Solution
Step 1: Understand the role of
Thetypedeftypedefkeyword is used to give a new name (alias) to an existing data type.Step 2: Differentiate from other keywords
It does not declare variables, define functions, or allocate memory; it only renames types.Final Answer:
To create a new name for an existing data type -> Option DQuick Check:
typedef= rename type [OK]
- Confusing typedef with variable declaration
- Thinking typedef allocates memory
- Assuming typedef defines functions
Byte for unsigned char using typedef?Solution
Step 1: Recall correct typedef syntax
The correct syntax is:typedef existing_type new_type_name;Step 2: Match syntax with options
typedef unsigned char Byte; matches this pattern exactly:typedef unsigned char Byte;Final Answer:
typedef unsigned char Byte; -> Option AQuick Check:
typedef syntax = typedef existing_type new_name [OK]
- Placing new type name before existing type
- Using '=' sign in typedef
- Swapping order of typedef and type
typedef int Number;
Number a = 5;
Number b = 10;
printf("%d\n", a + b);Solution
Step 1: Understand typedef alias usage
typedef int Number;meansNumberis another name forint. So variablesaandbare integers.Step 2: Calculate the sum of a and b
a = 5andb = 10, soa + b = 15. Theprintfprints this value.Final Answer:
15 -> Option BQuick Check:
5 + 10 = 15 [OK]
- Thinking typedef creates new data type behavior
- Confusing concatenation with addition
- Expecting compilation error due to typedef
typedef struct {
int x;
int y;
} Point;
Point p;
p.x = 10;
p.y = 20;
printf("%d %d", p.x, p.y);Solution
Step 1: Check struct typedef syntax
The struct is correctly defined withtypedef struct { ... } Point;and ends with a semicolon.Step 2: Verify variable declaration and usage
Point p;declares a variable of typePoint. Assigning values top.xandp.yis valid.Final Answer:
No error, code is correct -> Option AQuick Check:
Struct typedef and usage = correct [OK]
- Forgetting semicolon after typedef struct
- Trying to assign struct members without variable
- Misdeclaring struct variable
IntPtr to an int using typedef and then declare a variable p of that pointer type?typedef ??? IntPtr; IntPtr p; int x = 10; p = &x;
Solution
Step 1: Understand pointer typedef syntax
To create a pointer type alias, the asterisk (*) goes with the existing type:typedef int* IntPtr;Step 2: Check variable declaration and assignment
UsingIntPtr p;declarespas a pointer to int. Assigningp = &x;is valid.Final Answer:
typedef int* IntPtr; -> Option CQuick Check:
Pointer typedef = typedef int* new_name [OK]
- Placing * after new type name
- Using & instead of * in typedef
- Incorrect pointer syntax in typedef
Ready to prove it?
Pass the test and this pattern moves unknown
