What this does:

typedef struct {
    int a;
    int b;
} ab_t;

Is define an anonymous struct and give it the alias ab_t. For this case there's no problem as you can always use the alias. It would however be a problem if one of the members was a pointer to this type.

If for example you tried to do something like this:

typedef struct {
    int count;
    TNODE *left, *right;
} TNODE;

This wouldn't work because the type TNODE is not yet defined at the point it is used, and you can't use the tag of the struct (i.e. the name that comes after the struct keyword) because it doesn't have one.

Answer from dbush on Stack Overflow
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ structured-data-types-in-c-struct-and-typedef-explained-with-examples
Structured data types in C - Struct and Typedef Explained with Examples
February 1, 2020 - Structures and unions will give you the chance to store non-homogenous data types into a single collection. typedef struct student_structure{ char* name; char* surname; int year_of_birth; }student;
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-use-typedef-for-struct-in-c
How to use typedef for a Struct in C? - GeeksforGeeks
July 23, 2025 - In C, we use typedef to create aliases for already existing types. For structure, we can define a new name that can be used in place of the original struct name.
Discussions

How to properly use `typedef` for structs in C? - Stack Overflow
I see a lot of different typedef usages in many C courses and examples. Here is the CORRECT way to do this (example from ISO/IEC C language specification draft) typedef struct tnode TNODE; struct ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Whats the difference between a normal struct and a typedef struct?
A struct that isn't typedef'd requires you to use the keyword struct every time you use it as a type (so it'd be struct MyStruct mystruct;). This doesn't apply for structs you typedef. More on reddit.com
๐ŸŒ r/C_Programming
17
54
February 20, 2022
How to use the typedef struct in C?
This technique allows you to define a type name for your structures, making it easier to declare variables of that structure type later in your code. The typedef keyword in C is used to create an alias that can represent a type. More on designgurus.io
๐ŸŒ designgurus.io
1
10
June 25, 2024
Why should we typedef a struct so often in C? - Stack Overflow
I have seen many programs consisting of structures like the one below typedef struct { int i; char k; } elem; elem user; Why is it needed so often? Any specific reason or applicable area? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ Learning-programming-Why-should-we-typedef-a-struct-so-often-in-C
Learning programming: Why should we typedef a struct so often in C? - Quora
Alot of people who are not good ... and enum types called the tag namespace. Typedefing a struct, union, and/or enum type will have the typedef name placed in the global namespace....
Top answer
1 of 2
15

What this does:

typedef struct {
    int a;
    int b;
} ab_t;

Is define an anonymous struct and give it the alias ab_t. For this case there's no problem as you can always use the alias. It would however be a problem if one of the members was a pointer to this type.

If for example you tried to do something like this:

typedef struct {
    int count;
    TNODE *left, *right;
} TNODE;

This wouldn't work because the type TNODE is not yet defined at the point it is used, and you can't use the tag of the struct (i.e. the name that comes after the struct keyword) because it doesn't have one.

2 of 2
7

The difference between these two typedef declarations

typedef struct tnode TNODE;

struct tnode {
    int count;
    TNODE *left, *right;
};

TNODE s, *sp;

and

typedef struct {
    int a;
    int b;
} ab_t;

. is that in the second case you declared an unnamed structure. It means that within the structure you can not refer to itself. For example you can not write

typede struct {
    int count;
    TNODE *left, *right;
} TNODE;

because the name TNODE used in this member declaration

    TNODE *left, *right;

is not declared yet.

But you can refer to the structure if the structure tag will have a name like

struct tnode {
    int count;
    struct tnode *left, *right;
};

because the name struct tnode was already declared.

Another difference is that to declare a pointer to a structure there is no need to have a complete definition of the structure. That is you may write

typedef struct tnode TNODE;

TNODE *sp;

struct tnode {
    int count;
    TNODE *left, *right;
};

Pay attention to that you may write a typedef declaration also the following way

struct tnode {
    int count;
    struct tnode *left, *right;
} typedef TNODE;
๐ŸŒ
cppreference.com
en.cppreference.com โ€บ cpp โ€บ language โ€บ typedef
typedef specifier - cppreference.com
// simple typedef typedef unsigned long ulong; // the following two objects have the same type unsigned long l1; ulong l2; // more complicated typedef typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10]; // the following two objects have the same type int a1[10]; arr_t a2; // beware: the following two objects do not have the same type const intp_t p1 = 0; // int *const p1 = 0 const int *p2; // common C idiom to avoid having to write "struct S" typedef struct { int a; int b; } S, *pS; // the following two objects have the same type pS ps1; S* ps2; // error: storage-class-specifier cannot a
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_typedef.php
C typedef
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The typedef keyword lets you create a new name (an alias) for an existing type.
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ how-to-use-the-typedef-struct-in-c
How to use the typedef struct in C
For structures, the C typedef keyword offers us to define a new type name for a structure, making the code more readable and maintainable. Here is the syntax to use typedef for the structure data type: ... Using typedef struct results in a cleaner, ...
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_typedef.htm
Typedef in C
The C programming language provides a keyword called typedef to set an alternate name to an existing data type. The typedef keyword in C is very useful in assigning a convenient alias to a built-in data type as well as any derived data type ...
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ c-snippets โ€บ difference-between-typedef-struct-and-struct-definitions-with-example.php
C - Difference between typedef struct and struct
November 1, 2025 - In C language, struct is used to define a user-defined data type that groups together variables of different data types under a single name. A typedef can be used to create an alias for an existing data type, including a struct.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ typedef-in-c
C typedef - GeeksforGeeks
July 23, 2025 - Explanation: In this code, typedef is used to define an alias stu for the structure Students. The alias simplifies declaring variables of this structure type, such as st. The program then initializes and prints the values of the structure members ...
Top answer
1 of 16
590

As Greg Hewgill said, the typedef means you no longer have to write struct all over the place. That not only saves keystrokes, it also can make the code cleaner since it provides a smidgen more abstraction.

Stuff like

typedef struct {
  int x, y;
} Point;

Point point_new(int x, int y)
{
  Point a;
  a.x = x;
  a.y = y;
  return a;
}

becomes cleaner when you don't need to see the "struct" keyword all over the place, it looks more as if there really is a type called "Point" in your language. Which, after the typedef, is the case I guess.

Also note that while your example (and mine) omitted naming the struct itself, actually naming it is also useful for when you want to provide an opaque type. Then you'd have code like this in the header, for instance:

typedef struct Point Point;

Point * point_new(int x, int y);

and then provide the struct definition in the implementation file:

struct Point
{
  int x, y;
};

Point * point_new(int x, int y)
{
  Point *p;
  if((p = malloc(sizeof *p)) != NULL)
  {
    p->x = x;
    p->y = y;
  }
  return p;
}

In this latter case, you cannot return the Point by value, since its definition is hidden from users of the header file. This is a technique used widely in GTK+, for instance.

UPDATE Note that there are also highly-regarded C projects where this use of typedef to hide struct is considered a bad idea, the Linux kernel is probably the most well-known such project. See Chapter 5 of The Linux Kernel CodingStyle document for Linus' angry words. :) My point is that the "should" in the question is perhaps not set in stone, after all.

2 of 16
263

It's amazing how many people get this wrong. PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs.

Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.

Consider:

#ifndef FOO_H
#define FOO_H 1

#define FOO_DEF (0xDEADBABE)

struct bar; /* forward declaration, defined in bar.h*/

struct foo {
  struct bar *bar;
};

#endif

With such a definition, not using typedefs, it is possible for a compiland unit to include foo.h to get at the FOO_DEF definition. If it doesn't attempt to dereference the 'bar' member of the foo struct then there will be no need to include the "bar.h" file.

Also, since the namespaces are different between the tag names and the member names, it is possible to write very readable code such as:

struct foo *foo;

printf("foo->bar = %p", foo->bar);

Since the namespaces are separate, there is no conflict in naming variables coincident with their struct tag name.

If I have to maintain your code, I will remove your typedef'd structs.

๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ typedef-struct-in-c
typedef struct in C [Explained]
December 5, 2022 - At the same time that you declare a variable of structure type, you can set the initial value of each of its members. The initial value of each member is enclosed in { } and becomes the value of each member in the order in which the structure is defined. Hence, the typedef keyword helps user to provide meaningful names to the already existing data types in the C language and make it more understandable for others as well.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Typedef
typedef - Wikipedia
2 weeks ago - Here both C as well as C++ need the struct keyword in the parameter definition. The typedef may be used to define a new pointer type.
๐ŸŒ
mbedded.ninja
blog.mbedded.ninja โ€บ programming โ€บ languages โ€บ c โ€บ the-confusing-differences-between-struct-and-typedef-struct
The Confusing Differences Between struct and typedef struct | mbedded.ninja
October 30, 2022 - If you add typedef to the front of the struct definition like this: ... This changes the meaning of the word after the }, it now becomes an alias for the struct type, and not the name of a variable as it did above. What this does is save you having to type struct Person all the time, now you ...
Top answer
1 of 12
1361

The common idiom is using both:

typedef struct S { 
    int x; 
} S;

They are different definitions. To make the discussion clearer I will split the sentence:

struct S { 
    int x; 
};

typedef struct S S;

In the first line you are defining the identifier S within the struct name space (not in the C++ sense). You can use it and define variables or function arguments of the newly defined type by defining the type of the argument as struct S:

void f( struct S argument ); // struct is required here

The second line adds a type alias S in the global name space and thus allows you to just write:

void f( S argument ); // struct keyword no longer needed

Note that since both identifier name spaces are different, defining S both in the structs and global spaces is not an error, as it is not redefining the same identifier, but rather creating a different identifier in a different place.

To make the difference clearer:

typedef struct S { 
    int x; 
} T;

void S() { } // correct

//void T() {} // error: symbol T already defined as an alias to 'struct S'

You can define a function with the same name of the struct as the identifiers are kept in different spaces, but you cannot define a function with the same name as a typedef as those identifiers collide.

In C++, it is slightly different as the rules to locate a symbol have changed subtly. C++ still keeps the two different identifier spaces, but unlike in C, when you only define the symbol within the class identifier space, you are not required to provide the struct/class keyword:

 // C++
struct S { 
    int x; 
}; // S defined as a class

void f( S a ); // correct: struct is optional

What changes are the search rules, not where the identifiers are defined. The compiler will search the global identifier table and after S has not been found it will search for S within the class identifiers.

The code presented before behaves in the same way:

typedef struct S { 
    int x; 
} T;

void S() {} // correct [*]

//void T() {} // error: symbol T already defined as an alias to 'struct S'

After the definition of the S function in the second line, the struct S cannot be resolved automatically by the compiler, and to create an object or define an argument of that type you must fall back to including the struct keyword:

// previous code here...
int main() {
    S(); 
    struct S s;
}
2 of 12
228

struct and typedef are two very different things.

The struct keyword is used to define, or to refer to, a structure type. For example, this:

struct foo {
    int n;
};

creates a new type called struct foo. The name foo is a tag; it's meaningful only when it's immediately preceded by the struct keyword, because tags and other identifiers are in distinct name spaces. (This is similar to, but much more restricted than, the C++ concept of namespaces.)

A typedef, in spite of the name, does not define a new type; it merely creates a new name for an existing type. For example, given:

typedef int my_int;

my_int is a new name for int; my_int and int are exactly the same type. Similarly, given the struct definition above, you can write:

typedef struct foo foo;

The type already has a name, struct foo. The typedef declaration gives the same type a new name, foo.

The syntax allows you to combine a struct and typedef into a single declaration:

typedef struct bar {
    int n;
} bar;

This is a common idiom. Now you can refer to this structure type either as struct bar or just as bar.

Note that the typedef name doesn't become visible until the end of the declaration. If the structure contains a pointer to itself, you have use the struct version to refer to it:

typedef struct node {
    int data;
    struct node *next; /* can't use just "node *next" here */
} node;

Some programmers will use distinct identifiers for the struct tag and for the typedef name. In my opinion, there's no good reason for that; using the same name is perfectly legal and makes it clearer that they're the same type. If you must use different identifiers, at least use a consistent convention:

typedef struct node_s {
    /* ... */
} node;

(Personally, I prefer to omit the typedef and refer to the type as struct bar. The typedef saves a little typing, but it hides the fact that it's a structure type. If you want the type to be opaque, this can be a good thing. If client code is going to be referring to the member n by name, then it's not opaque; it's visibly a structure, and in my opinion it makes sense to refer to it as a structure. But plenty of smart programmers disagree with me on this point. Be prepared to read and understand code written either way.)

(C++ has different rules. Given a declaration of struct blah, you can refer to the type as just blah, even without a typedef. Using a typedef might make your C code a little more C++-like -- if you think that's a good thing.)

๐ŸŒ
Medium
medium.com โ€บ @Dev_Frank โ€บ typedef-dc45fa2a0794
Typedef. Typedef is a keyword used to create anโ€ฆ | by Dev Frank | Medium
May 22, 2024 - typedef struct { int width; int height; } Rectangle; Rectangle rect1, rect2; Here, Rectangle is an alias for the unnamed structure. Variables rect1 and rect2 are declared as Rectangle. Itโ€™s common to use typedef to create an alias for a pointer to a structure, which can make code involving pointers more readable:
๐ŸŒ
Facebook
facebook.com โ€บ groups โ€บ cs50 โ€บ posts โ€บ 775584965921884
Any explanation of typedef and struct in C programming?
I am sorry i forgot this part.. when declaring our own data type... we use (typedef struct) . what does each one do ? I am guessing typedef is declaring that we are going to define a new type and...
๐ŸŒ
The Coding Forums
thecodingforums.com โ€บ archive โ€บ archive โ€บ c programming
typedefing a struct | C Programming | Coding Forums
September 14, 2007 - No, but there is sometimes reason to do it the other way round: typedef struct mystruct mystruct; struct mystruct { int a; int b; }; Because you can then use the type name "mystruct" in and before the definition of the struct, which is useful if you have structures that point to each other.