Difference Between Struct and Typedef Struct in C

Mehvish Ashiq Oct 12, 2023
  1. Structure in C
  2. Difference Between struct and typedef struct in C
  3. Key Points When Using the typedef Keyword
Difference Between Struct and Typedef Struct in C

This tutorial will explain and demonstrate the difference between struct and typedef struct in C programming with the help of code examples.

Structure in C

When using arrays, we define the variables’ type that holds many data items of the same type/kind. Similarly, we have another user-defined data type in C programming, known as a structure, which lets us combine different types of data items.

We can also represent a record using structures. For instance, we can keep track of the person’s details where each person has the following attributes:

  • First Name
  • Last Name
  • Gender
  • Age
  • Mobile
  • Email
  • Address

Difference Between struct and typedef struct in C

We can define a structure using struct and typedef struct, but the typedef keyword lets us write alternative names for the user-defined data types (e.g., struct) and primitive data types (e.g., int).

The typedef keyword creates a brand-new name to an already existing data type but does not create the new data type. We can have a cleaner and more readable code if we use the typedef struct, and it also saves us (the programmer) from keystrokes.

The typedef struct can simplify declaring variables, providing the equivalent code with simplified syntax. However, it may lead to a more cluttered global namespace, which can cause problems for more extensive programs.

Let’s have a sample code for both scenarios (the struct and the typedef struct) and understand the difference.

Example Code Without the typedef Keyword

#include <stdio.h>

struct Books {
  int id;
  char author[50];
  char title[50];
};

int main() {
  // declare `book1` and `book2` of type `Books`
  struct Books book1;
  struct Books book2;

  // the specifications for the `book1`
  book1.id = 1;
  strcpy(book1.author, "Zara Ali");
  strcpy(book1.title, "Tutorials for C Programming");

  // the specifications for the `book2`
  book2.id = 2;
  strcpy(book2.author, "Zaid Ali");
  strcpy(book2.title, "Tutorials for Java Programming");

  // display information for `book1` and `book2`
  printf("The id of book1: %d\n", book1.id);
  printf("The author of the book1: %s\n", book1.author);
  printf("The title of the book1: %s\n", book1.title);

  printf("The id of book2: %d\n", book2.id);
  printf("The author of the book2: %s\n", book2.author);
  printf("The title of the book2: %s\n", book2.title);

  return 0;
}

Output:

The id of book1: 1
The author of the book1: Zara Ali
The title of the book1: Tutorials for C Programming
The id of book2: 2
The author of the book2: Zaid Ali
The title of the book2: Tutorials for Java Programming

The above code has two variables, book1 and book2, of type Books. We will have to type struct again and again if we are required to declare more variables, e.g., book3, book4, etc.

This is where the typedef struct comes into the picture. See the following code snippet showing the use of typedef struct.

Example Code With the typedef Keyword

We can use the typedef in the following two methods.

Method 1:

#include <stdio.h>

struct Books {
  int id;
  char author[50];
  char title[50];
};

typedef struct Books Book;

int main() {
  // declare `book1` and `book2` of type `Book`
  Book book1;
  Book book2;

  // the specifications for the `book1`
  book1.id = 1;
  strcpy(book1.author, "Zara Ali");
  strcpy(book1.title, "Tutorials for C Programming");

  // the specifications for the `book2`
  book2.id = 2;
  strcpy(book2.author, "Zaid Ali");
  strcpy(book2.title, "Tutorials for Java Programming");

  // display information for `book1` and `book2`
  printf("The id of book1: %d\n", book1.id);
  printf("The author of the book1: %s\n", book1.author);
  printf("The title of the book1: %s\n", book1.title);

  printf("The id of book2: %d\n", book2.id);
  printf("The author of the book2: %s\n", book2.author);
  printf("The title of the book2: %s\n", book2.title);

  return 0;
}

Output:

The id of book1: 1
The author of the book1: Zara Ali
The title of the book1: Tutorials for C Programming
The id of book2: 2
The author of the book2: Zaid Ali
The title of the book2: Tutorials for Java Programming

Method 2:

#include <stdio.h>

typedef struct Books {
  int id;
  char author[50];
  char title[50];
} Book;

int main() {
  // declare `book1` and `book2` of type `Book`
  Book book1;
  Book book2;

  // the specifications for the `book1`
  book1.id = 1;
  strcpy(book1.author, "Zara Ali");
  strcpy(book1.title, "Tutorials for C Programming");

  // the specifications for the `book2`
  book2.id = 2;
  strcpy(book2.author, "Zaid Ali");
  strcpy(book2.title, "Tutorials for Java Programming");

  // display information for `book1` and `book2`
  printf("The id of book1: %d\n", book1.id);
  printf("The author of the book1: %s\n", book1.author);
  printf("The title of the book1: %s\n", book1.title);

  printf("The id of book2: %d\n", book2.id);
  printf("The author of the book2: %s\n", book2.author);
  printf("The title of the book2: %s\n", book2.title);

  return 0;
}

Output:

The id of book1: 1
The author of the book1: Zara Ali
The title of the book1: Tutorials for C Programming
The id of book2: 2
The author of the book2: Zaid Ali
The title of the book2: Tutorials for Java Programming

The second method shows that using typedef seems more organized, clean, and easy to understand and manage.

Key Points When Using the typedef Keyword

We must remember a few points while using the typedef keyword. In the code given above, we are defining the structure as follows.

typedef struct Books {
  int id;
  char author[50];
  char title[50];
} Book;

Here, we have a new structure type, struct Books, and the alias named Book for the struct Books. You might be wondering why the alias name with the typedef appears at the declaration’s end like any other ordinary declaration.

The typedef was put in the exact specifier category as the storage-class specifiers, for instance, auto and static. We can also declare without the tag name if we only want to use the Book type identifier.

typedef struct {
  int id;
  char author[50];
  char title[50];
} Book;

While using structures, we must take care of what we should call a variable and an alias. If we use the anonymous structure as follows (anonymous because the structure has no name), then the Book would be the user-defined data type structure variable.

struct {
  int id;
  char author[50];
  char title[50];
} Book;

If we use the same structure given above but with the typedef keyword, then the Book would be an alias (synonym) to the data type structure, and we can use that to declare variables (see the following).

typedef struct {
  int id;
  char author[50];
  char title[50];
} Book;
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - C Struct