Concept Flow - Typedef keyword
Write typedef statement
Compiler creates new type name
Use new type name in code
Code compiles with alias type
The typedef keyword creates a new name (alias) for an existing type, making code easier to read and write.
typedef unsigned int uint; uint age = 30; printf("Age: %u\n", age);
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Read typedef unsigned int uint; | Create alias 'uint' for 'unsigned int' | Alias 'uint' ready to use |
| 2 | Declare variable 'age' of type 'uint' | uint means unsigned int | Variable 'age' of type unsigned int created with value 30 |
| 3 | Print value of 'age' | age = 30 | Output: Age: 30 |
| 4 | End of program | No more statements | Program ends successfully |
| Variable | Start | After Declaration | Final |
|---|---|---|---|
| age | undefined | 30 | 30 |
typedef creates a new name for an existing type Syntax: typedef existing_type new_name; Use new_name as a type in declarations Helps make code clearer and easier to read Does not create a new type, just an alias
typedef keyword in C?typedeftypedef keyword is used to give a new name (alias) to an existing data type.typedef = rename type [OK]Byte for unsigned char using typedef?typedef existing_type new_type_name;typedef unsigned char Byte;typedef int Number;
Number a = 5;
Number b = 10;
printf("%d\n", a + b);typedef int Number; means Number is another name for int. So variables a and b are integers.a = 5 and b = 10, so a + b = 15. The printf prints this value.typedef struct {
int x;
int y;
} Point;
Point p;
p.x = 10;
p.y = 20;
printf("%d %d", p.x, p.y);typedef struct { ... } Point; and ends with a semicolon.Point p; declares a variable of type Point. Assigning values to p.x and p.y is valid.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;
typedef int* IntPtr;IntPtr p; declares p as a pointer to int. Assigning p = &x; is valid.Pass the test and this pattern moves unknown