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.

Answer from unwind on Stack Overflow
๐ŸŒ
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 ... The typedef keyword lets you create a new name (an alias) for an existing type.
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.

Discussions

programming languages - Why use typedefs for structs? - Software Engineering Stack Exchange
Most arguments center on the idea that typedefs simply hide the actual data type of a variable. For example, Greg Kroah-Hartman, a Linux kernel hacker and documenter, discourages their use for anything except function prototype declarations. He argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
April 23, 2013
typedefing a struct
Is there any functional difference (or any other reason to prefer one over the other) between these two methods: typedef struct mystruct { int a... More on thecodingforums.com
๐ŸŒ thecodingforums.com
15
September 14, 2007
Is it bad to use typedef?
Programming languages allow you to use the principle of abstraction to define solutions to problems. Typedefs in C are nothing more than syntactic sugar which allows you to define a type synonym for a complex object. You use these type synonyms to make your code more readable and more understandable. It's the same reason you define functions. You want to replace a complex sequence of symbols with a simpler, more meaningful symbol. The true test for whether or not your code is easy to read and maintain is if an equally skilled individual can understand the code you've written. The same applies to you, the author, if you cannot understand the code you've written 6 to 12 months after you last touched it, that's proof that your code was not clearly written. More on reddit.com
๐ŸŒ r/C_Programming
58
61
June 27, 2020
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
๐ŸŒ
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.
๐ŸŒ
CodersLegacy
coderslegacy.com โ€บ home โ€บ learn c++ โ€บ how (and why) to use typedef struct in c
How (and why) to use typedef struct in C - CodersLegacy
February 16, 2022 - Some people may not want to have to write the โ€œstructโ€ part every time you create a new struct variable. So in order to resolve this problem, we use typedef.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_files_read.php
C Read Files
C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-typedef-struct-and-struct-in-C-programming
What is the difference between typedef struct and struct in C programming? - Quora
Answer (1 of 3): The C language standard mandates separate namespaces for different categories of identifiers, including tag identifiers (for [code ]struct[/code]/[code ]union[/code]/[code ]enum[/code]) and ordinary identifiers (for [code ]typedef[/code] and other identifiers). typedef is a keyw...
๐ŸŒ
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 ...
๐ŸŒ
CSE CGI Server
cgi.cse.unsw.edu.au โ€บ ~cs1521 โ€บ 26T1 โ€บ assignments โ€บ ass2 โ€บ index.html
COMP1521 26T1 โ€” Assignment 2: Standard Input/Output Library
February 28, 2026 - You should implement struct file, which is currently defined as an empty struct in cs1521_stdio.c. You should only need to include a field for the file descriptor. However, later in the assignment, you will likely need to add additional fields. In cs1521_stdio.h, we have included a typedef for the type struct file:
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ how-to-use-the-typedef-struct-in-c
How to use the typedef struct in C
May 19, 2025 - Note that if we use the typedef keyword with a struct, there is no longer a need to type struct again and again with every declaration of the variable of this type. In the code below, the structure Point is defined separately using struct Point, and then a typedef is applied to create an alias Point for this structure.
๐ŸŒ
Crustc
crustc.com โ€บ home โ€บ typedef struct vs struct definitions in c
Typedef Struct vs Struct Definitions in C - crustc
July 24, 2023 - While struct is incredible, it can become cumbersome to constantly write struct before every instance of our defined type, typedef solves this issue. typedef is a keyword in C that allows us to create an alias for existing data types.
๐ŸŒ
PlantUML
plantuml.com โ€บ home โ€บ language specification โ€บ class diagram
Class Diagram syntax and features
A class diagram describes the static structure of a system: its classes, their attributes and methods, and the relationships between them.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Struct_(C_programming_language)
struct (C programming language) - Wikipedia
3 days ago - However, some programming style guides advise against this, claiming that it can obfuscate the type. ... typedef struct MyStruct { Type1 member1; Type2 member2; } Thing; // struct MyStruct can now be referred to as 'Thing' Thing thing;
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
> Wrap your structs in a typedef NOOoooooo, please stop doing this. unless you a... | Hacker News
January 8, 2023 - NOOoooooo, please stop doing this. unless you are a library author who gets to define things like uint8_t, please do not do this ยท even BIGGER no. This is completely misunderstanding what a typedef is and what it should be used for
๐ŸŒ
Binaryupdates
binaryupdates.com โ€บ home โ€บ structures, typedef and union in c programming
Structures, Typedef and Union in C Programming Language
August 4, 2021 - Letโ€™s assign name to variable ... applies to all other member variables. ... The keyword Typedef is used to give a new symbolic name for the existing name....
๐ŸŒ
The Coding Forums
thecodingforums.com โ€บ archive โ€บ archive โ€บ c programming
typedefing a struct | C Programming | Coding Forums
September 14, 2007 - Note that in both cases, any reference to the type within its own definition (say, if a mystruct contains a pointer to a mystruct) has to use the name 'struct mystruct', since the typedef name doesn't exist yet. You could even drop the tag, and just declare typedef struct { int a; int b; } mystruct; since you never use the struct tag name anyway.
Top answer
1 of 6
2
Note: please don't typedef structs to date_t, those names ending in _t are reserved for the operating system headers. Better yet, don't typedef structs - it pollutes the global namespace while only providing some mild abstraction in case you don't want it to be a struct at some point in the future.
2 of 6
0
**struct** is a way to describe the **layout of a block of memory**. Declaring: ``struct date { short year; short month; short day; };`` is a way to tell the person reading your code how you represent dates in memory. **typedef** provides a higher degree of **abstraction** when reading the code. If you declare: ``typedef struct { short year; short month; short day; } date_t;`` you'll be able to declare a date variable using 'date_t dtStart;" instead of 'struct date dtStart;' If you access the year, month and day fields directly, it doesn't change much. However, if you use macros to get these fields, your code become **decoupled** from the implementation of the date_t type. _example1.h_ ``typedef struct { short year; short month; short day; } date_t; #define DAY(d) d.day #define MONTH(d) d.month #define YEAR(d) d.year`` _example1.c:_ ``date_t dtStart; // ... printf("Start date: %02d/%02d/%4d\n", MONTH(dtStart), DAY(dtStart), YEAR(dtStart));`` _example2.h: => very different from example1.h_ ``typedef unsigned long date_t; #define DAY(d) (d % 100) #define MONTH(d) (d / 100 % 100) #define YEAR(d) (d / 10000)`` _example2.c: => identical to example1.c!_ ``date_t dtStart; // ... printf("Start date: %02d/%02d/%4d\n", MONTH(dtStart), DAY(dtStart), YEAR(dtStart));`` While struct and typedef represent data, **functions** represent **code**. struct and typedef can be used to describe the arguments and/or the return type of a function. Example: ``int compare_dates(date_t *d1, date_t *d2) { if (YEAR(*d1) < YEAR(*d2) { return -1; } else { // ... }``