Programming
Struct Definitions
Typedef
C Programming
Code Comparisons

typedef struct vs struct definitions

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C, struct and typedef struct do not create two different kinds of data structures. They describe the same underlying type. The difference is mostly about naming, syntax, and how you want the type to be used throughout the codebase.

The key C-specific detail is that a struct tag and a typedef name live in different naming spaces. That is why struct Point and Point are not automatically the same thing unless you explicitly connect them with typedef.

Plain struct Requires the struct Keyword

A normal struct definition looks like this:

c
1struct Point {
2    int x;
3    int y;
4};
5
6int main(void) {
7    struct Point p;
8    p.x = 10;
9    p.y = 20;
10    return 0;
11}

In C, the type name here is struct Point, not just Point. That is why the declaration must repeat the struct keyword.

This style is perfectly valid and common in codebases that want to emphasize the distinction between the tag and other type names.

typedef struct Creates an Alias

If you want to write Point instead of struct Point, add a typedef:

c
1typedef struct Point {
2    int x;
3    int y;
4} Point;
5
6int main(void) {
7    Point p;
8    p.x = 10;
9    p.y = 20;
10    return 0;
11}

Now both names exist:

  • 'struct Point is the tagged struct type'
  • 'Point is a typedef alias for that same type'

This is mostly a convenience and style choice, not a semantic change to the structure itself.

Anonymous Struct With a Typedef

You can also skip the tag and expose only the typedef name:

c
1typedef struct {
2    int x;
3    int y;
4} Point;

This is concise, but it removes the struct tag. That matters if you want forward declarations or opaque pointer patterns later.

Forward Declarations and Opaque Types

One reason tagged structs remain useful is forward declaration:

c
struct Node;

struct Node *create_node(int value);

This works because the tag struct Node exists even before the full definition appears. It is especially useful in header files when you want to hide implementation details.

A common opaque-pointer style looks like this:

c
1typedef struct Database Database;
2
3Database *database_open(const char *path);
4void database_close(Database *db);

Then the full struct definition stays in the .c file. That is much harder to express cleanly if you only used an anonymous typedef struct.

Why Style Choices Differ Between C and C++

In C++, the struct name becomes a usable type name directly, so struct Point { ... }; Point p; works without an extra typedef. In C, it does not.

That is why many C programmers like typedef struct for readability, while others prefer plain struct to make it obvious that the type is a struct and to preserve tag-based patterns cleanly.

Neither is universally correct. The better choice depends on the codebase style and whether forward declarations and opaque interfaces matter.

Common Pitfalls

The biggest mistake is thinking typedef struct creates a different runtime layout or different semantics. It does not. It only introduces an alias.

Another common issue is using an anonymous typedef struct and later needing a forward declaration or opaque handle pattern. Without a tag, that becomes awkward.

Developers also sometimes mix styles inconsistently in the same codebase, which makes APIs harder to read. Pick a convention and apply it consistently.

Finally, be careful when comparing C and C++ examples. A style that is optional in C++ may be required or more meaningful in C.

Summary

  • 'struct and typedef struct describe the same underlying C structure type.'
  • Plain struct requires declarations such as struct Point p;.
  • 'typedef struct ... Point; lets you write Point p; instead.'
  • Tagged structs are better for forward declarations and opaque API patterns.
  • The choice is mostly about naming and codebase style, not about memory layout or performance.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.