What is the use of typedef keyword in C ? When is it needed?
8 Answers
typedef
is for defining something as a type. For instance:
typedef struct {
int a;
int b;
} THINGY;
...defines THINGY
as the given struct. That way, you can use it like this:
THINGY t;
...rather than:
struct _THINGY_STRUCT {
int a;
int b;
};
struct _THINGY_STRUCT t;
...which is a bit more verbose. typedefs can make some things dramatically clearer, specially pointers to functions.

- 1,031,962
- 187
- 1,923
- 1,875
-
3Is there ever a reason to not `typedef` a `struct` then? – Panzercrisis Sep 28 '14 at 04:37
-
3@Panzercrisis: I'm not aware of one, certainly I was doing it routinely ~1989-1998 (the years I did most of my C work), but I'm not a C expert (anymore). – T.J. Crowder Sep 28 '14 at 08:08
-
But typically all the sub types of the `struct` would be `typedef`s of simple types like `short` or `double` and some times those are `typedef`s of `typedef`s which sends you on a rabbit hunt across various `.h` files by the end of which you've forgotten what it was used for, and you have to do this for _every variable_. They are a nightmare! – CpILL Apr 25 '19 at 00:28
From wikipedia:
typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.
And:
K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration easier to understand.
And an argument against:
He (Greg K.H.) argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types.

- 489,969
- 99
- 883
- 1,009
-
I totally agree with Greg K.H. see my comment under @T.J.Crowder. I think its bad practice and they should be used minimally – CpILL Apr 25 '19 at 00:30
Typedef is used to create aliases to existing types. It's a bit of a misnomer: typedef does not define new types as the new types are interchangeable with the underlying type. Typedefs are often used for clarity and portability in interface definitions when the underlying type is subject to change or is not of importance.
For example:
// Possibly useful in POSIX:
typedef int filedescriptor_t;
// Define a struct foo and then give it a typedef...
struct foo { int i; };
typedef struct foo foo_t;
// ...or just define everything in one go.
typedef struct bar { int i; } bar_t;
// Typedef is very, very useful with function pointers:
typedef int (*CompareFunction)(char const *, char const *);
CompareFunction c = strcmp;
Typedef can also be used to give names to unnamed types. In such cases, the typedef will be the only name for said type:
typedef struct { int i; } data_t;
typedef enum { YES, NO, FILE_NOT_FOUND } return_code_t;
Naming conventions differ. Usually it's recommended to use a trailing_underscore_and_t
or CamelCase
.

- 577
- 1
- 6
- 18
-
As described here naming your own types with _t isn't recommended: https://stackoverflow.com/a/231807/2985095 – Gregory Arenius Aug 15 '18 at 00:03
Explaining the use of typedef in the following example. Further, Typedef is used to make the code more readable.
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv[])
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %f\n", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
} In front of everything, place the keyword typedef.
*/

- 3,110
- 2
- 20
- 19
-
`typedef`'n standard types like `double` is just confusing when reading the code, is it a struct or an array or what? You then have to go hunting for where it is defined and memorise possibly hundreds of new names for standard types all of which can be named the same way. In the end you need to know what the underlying type is in order to use the variable correctly. – CpILL Apr 25 '19 at 00:36
-
@CpILL, although I agree with what you stated, I think you missed an important point. Imagine you are programming a network stack, and you want to a data structure (in C) to represent a node with all its associated characteristics. In this case, naming a struct that contains all the needed variables as "node" is very convenient. Have a look at how I'm using it [here](https://github.com/amjadmajid/Backscatter-Network/blob/master/T2Tnet/sys/inc/custom_data_type.h) – Amjad Jun 07 '19 at 11:49
-
Your hiding your struct behind a `typedef` which is exactly my point! when your declaring a variable it should be obvious what type it is so `struct RBuf buff;` is easier to understand than `rbuf_t buff;` cos what the hell is a `rbuf_t`? One has to dig around for it and then memorise it just to work with your code. – CpILL Jun 11 '19 at 05:39
typedef doesnot introduce a new type but it just provide a new name for a type.
TYPEDEF
CAN BE USED FOR:
Types that combine arrays,structs,pointers or functions.
To facilitate the portability ,
typedef
the type you require .Then when you port the code to different platforms,select the right type by making changes only in the typedef.A
typedef
can provide a simple name for a complicated type cast.typedef
can also be used to give names to unnamed types. In such cases, the typedef will be the only name for said type.
NOTE:-SHOULDNT USE TYPEDEF
WITH STRUCTS. ALWAYS USE A TAG IN A STRUCTURE DEFINITION EVEN IF ITS NOT NEEDED.

- 545
- 6
- 15
from Wikipedia: "K&R states that there are two reasons for using a typedef. First ... . Second, a typedef can make a complex declaration easier to understand."
Here is an example of the second reason for using typedef, simplifying complex types (the complex type is taken from K&R "The C programming language second edition p. 136).
char (*(*x())[])()
x is a function returning pointer to array[] of pointer to function returning char.
We can make the above declaration understandable using typedefs. Please see the example below.
typedef char (*pfType)(); // pf is the type of pointer to function returning
// char
typedef pfType pArrType[2]; // pArr is the type of array of pointers to
// functions returning char
char charf()
{ return('b');
}
pArrType pArr={charf,charf};
pfType *FinalF() // f is a function returning pointer to array of
// pointer to function returning char
{
return(pArr);
}

- 33
- 1
- 7

- 124
- 3
It can alias another type.
typedef unsigned int uint; /* uint is now an alias for "unsigned int" */

- 44,018
- 30
- 122
- 156
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;

- 11,201
- 10
- 62
- 89

- 457
- 3
- 13