🌐
W3Schools
w3schools.com β€Ί c β€Ί c_structs.php
C Structures (structs)
C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding
🌐
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.
Discussions

c - typedef struct vs struct definitions - Stack Overflow
I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
W3Schools
w3schools.in β€Ί c-programming β€Ί typedef
C typedef - W3Schools
C is such a dominant language of ... type by combining the data type and its qualifiers. typedef is a C keyword implemented to tell the compiler to assign alternative names to C's pre-existing data types....
🌐
Educative
educative.io β€Ί blog β€Ί how-to-use-the-typedef-struct-in-c
How to use the typedef struct in C
Learn how to use typedef struct in C to simplify complex data type declarations and improve code readability.
🌐
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.
🌐
W3Schools Blog
w3schools.blog β€Ί home β€Ί typedef c struct
typedef c struct - W3schools.blog
June 30, 2022 - [ad_1] typedef c struct /* Method one */ typedef struct Vertex { int x; int y; } Point; /* Method two */ struct Vertex { int x; int y; } typedef struct Vertex Point; [ad_2] Please follow and like us:
🌐
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 ...
🌐
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.
Find elsewhere
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.)

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;
🌐
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.
🌐
TechVidvan
techvidvan.com β€Ί tutorials β€Ί c-typedef-with-examples
C Typedef with Examples - TechVidvan
August 11, 2021 - TechVidvan Tutorial: typedef using struct!Salary of the first employee is : 14000 ID of the first employee is : 1 Salary of the second employee is : 12000 ID of the second employee is : 2 Β· In the above example, we have declared variable emp of type struct employee. We can create variables using the emp variable of type struct employee.
🌐
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 - To access a structure variable you can use the point like in stu.name. There is also a shorter way to assign values to a structure: typedef struct{ int x; int y; }point; point image_dimension = {640,480};
🌐
W3Schools
w3schools.in β€Ί c-programming β€Ί structures
C Structures - W3Schools
#include<stdio.h> #include<string.h> struct Courses { char WebSite[50]; char Subject[50]; int Price; }; void main( ) { struct Courses C; // Initialization strcpy( C.WebSite, "w3schools.in"); strcpy( C.Subject, "The C Programming Language"); C.Price = 0; // Print printf( "WebSite : %s\n", C.WebSite); printf( "Book Author : %s\n", C.Subject); printf( "Book Price : %d\n", C.Price); }
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.

🌐
W3Schools
w3schools.com β€Ί c β€Ί ref_keyword_struct.php
C struct Keyword
C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example
🌐
W3Schools
w3schools.com β€Ί c β€Ί c_data_types.php
C Data Types
C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding
🌐
Learn C
learnc.net β€Ί home β€Ί learn c programming β€Ί c typedef
C typedef
April 13, 2025 - You can use typedef with union. Suppose your program allows users to use either username or email to login. You can define account struct with account_name union as follows:
🌐
guvi.in
studytonight.com β€Ί c β€Ί typedef.php
typedef Keyword in C Programming
typedef struct { type member1; type member2; type member3; } type_name; Here type_name represents the stucture definition associated with it. Now this type_name can be used to declare a variable of this stucture type.