C Typedef
# C typedef
The C language provides the **typedef** keyword, which you can use to give a new name to a type. The following example defines the term **BYTE** for a single-byte number:
typedef unsigned char BYTE;
After this type definition, the identifier BYTE can be used as an abbreviation for the type **unsigned char**, for example:
BYTE b1, b2;
By convention, uppercase letters are used in the definition to remind the user that the type name is a symbolic abbreviation, but you can also use lowercase letters, as shown below:
typedef unsigned char byte;
You can also use **typedef** to give a new name to a user-defined data type. For example, you can use typedef with a structure to define a new data type name, and then use this new data type to directly define structure variables, as shown below:
## Example
```c
#include
#include
typedef struct Books
{
char title;
char author;
char subject;
int book_id;
} Book;
int main( )
{
Book book;
strcpy( book.title, "C Tutorial");
strcpy( book.author, "");
strcpy( book.subject, "programming language");
book.book_id = 12345;
printf( "book title : %sn", book.title);
printf( "book author : %sn", book.author);
printf( "book category : %sn", book.subject);
printf( "book ID : %dn", book.book_id);
return 0;
}
When the above code is compiled and executed, it produces the following result:
book title : C Tutorial
book author :
book category : programming language
book ID : 12345
## typedef vs #define
**#define** is a C directive used to define aliases for various data types, similar to **typedef**, but they have the following differences:
* **typedef** is limited to defining symbolic names for types, while **#define** can not only define aliases for types but also for values. For example, you can define 1 as ONE.
* **typedef** is interpreted by the compiler, while **#define** statements are processed by the preprocessor.
Here is the simplest usage of #define:
## Example
```c
#include
#define TRUE 1
#define FALSE 0
int main( )
{
printf( "TRUE value: %dn", TRUE);
printf( "FALSE value: %dn", FALSE);
return 0;
}
When the above code is compiled and executed, it produces the following result:
TRUE value: 1
FALSE value: 0
## 4 Notes β Write a Note
1. #0 Learn to Relieve Worries
145***3462@qq.com [](#)319 **Differences between typedef and #define**
(1) #define can use other type specifiers to extend the macro type name, but this cannot be done with type names defined by typedef. For example:
```c
#define INTERGE int;
unsigned INTERGE n; // No problem
typedef int INTERGE;
unsigned INTERGE n; // Error, cannot add unsigned before INTERGE
(2) When defining several variables consecutively, typedef can ensure that all defined variables are of the same type, while #define cannot guarantee this. For example:
```c
#define PTR_INT int *
PTR_INT p1, p2; // p1 and p2 are not the same type; after macro expansion it becomes: int *p1, p2;
typedef int * PTR_INT;
PTR_INT p1, p2; // p1 and p2 are the same type; they are both pointers to int.
(#)
145***3462@qq.com 8 years ago (2018-02-22)
2. #0 Barking Mad Dog
342***965@qq.com [](#)97
**Comparison of typedef and #define**
Some features of typedef overlap with the functionality of define. For example:
```c
#define BYTE unsigned char
This is the preprocessor replacing BYTE with unsigned char.
However, there are capabilities that #define does not have, for example:
```c
typedef char * STRING;
The compiler interprets STRING as an identifier for a type that points to char. Therefore:
```c
STRING name, sign;
is equivalent to:
```c
char * name, * sign;
However, if we assume:
```c
#define STRING char *
Then the following declaration:
```c
STRING name, sign;
will be translated into:
```c
char * name, sign;
This results in only name being a pointer.
In short, #define is merely a literal substitution performed by the preprocessor. `#define A B` is equivalent to opening the editor's find-and-replace function and replacing all occurrences of B with A.
Unlike #define, typedef has the following three characteristics:
* 1. The symbolic names given by typedef are limited to types, not values.
* 2. The interpretation of typedef is performed by the compiler, not the preprocessor. It is not a simple text replacement.
* 3. Although its scope is limited, typedef is more flexible than #define within its restricted scope.
(javascript:;)
342***965@qq.com
8 years ago (2018-05-21)
3. #0 A Little Book Boy
133***2011@qq.com [](#)106 Using **typedef** to create an alias for an array:
```c
typedef int A;
This means using **A** in place of **int **.
That is: `A a;` is equivalent to `int a;`
(javascript:;)
133***2011@qq.com 8 years ago (2018-07-20)
4. #0 CoolLoser
103***3350@qq.com [](#)174 **typedef also has another purpose: defining a new, simple alias for a complex declaration. It is particularly useful in callback functions:**
1. Original declaration: `int *(*a)(int, char*);`
Here, the variable name is **a**. Simply replace **a** with a new alias **pFun**:
```c
typedef int *(*pFun)(int, char*);
So the simplified version of the original declaration becomes:
```c
pFun a;
2. Original declaration: `void (*b) (void (*)());`
Here, the variable name is b. First replace the part inside the parentheses on the right side, with pFunParam as alias one:
```c
typedef void (*pFunParam)();
Then replace the variable **b** on the left side, with **pFunx** as alias two:
```c
typedef void (*pFunx)(pFunParam);
So the simplified version of the original declaration becomes:
```c
pFunx b;
Actually, it can be understood this way:
```c
typedef int *(*pFun)(int, char*);
YouTip