Storage Class #5: typedef
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program to Use typedef with a struct to Create a Complex Data Type Alias

📑 On this page:
  • What Does typedef Do?
  • Simple Rules to Remember
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • What typedef actually does, in plain words
  • How to give a struct a short, friendly name like Complex
  • Why typedef makes struct code easier to read
  • The two ways to write the typedef (tagged struct vs anonymous struct)
  • Why typedef doesn't create a new type — it only creates a new name

What Does typedef Do?

typedef gives a nickname to an existing type. It doesn't create a new type — it just lets you use a shorter, cleaner name for one that already exists.

Structs are the classic example. Without typedef, you'd have to write the keyword struct every time:

struct Complex z1;      /* verbose */
struct Complex z2;      /* tedious */
                                

With typedef, you can write:

Complex z1;             /* clean */
Complex z2;             /* easy */
                                

Same type, same memory, same behavior — just a friendlier name.

💡 In one line: typedef is a shortcut. It gives a type a new name — the type itself doesn't change.

C program using typedef with a struct to create a Complex data type alias

Simple Rules to Remember

Question Answer
Creates a new type?No — just a new name for an existing type
Costs memory at runtime?No — it's purely compile-time
Where is it declared?Usually at the top of the file, before any use
Can I typedef a pointer?Yes, e.g. typedef struct Node *NodePtr;
Can I typedef a function?Yes, e.g. typedef int (*Callback)(int);

Complete C Program

Program: A Complex number type made readable with typedef

#include <stdio.h>

/* Step 1: raw struct with a tag */
struct Complex {
    double real;
    double imag;
};

/* Step 2: typedef alias - makes "Complex" usable without the struct keyword */
typedef struct Complex Complex;

/* Function that takes Complex by value */
void printComplex(Complex z, const char *label) {
    printf("%s = %.2f + %.2fi\n", label, z.real, z.imag);
}

/* Function that adds two Complex numbers */
Complex addComplex(Complex a, Complex b) {
    Complex result;
    result.real = a.real + b.real;
    result.imag = a.imag + b.imag;
    return result;
}

int main(void) {
    Complex z1;
    Complex z2;
    Complex sum;

    z1.real = 3.0;
    z1.imag = 4.0;

    z2.real = 1.5;
    z2.imag = 2.5;

    printComplex(z1, "z1");
    printComplex(z2, "z2");

    sum = addComplex(z1, z2);
    printComplex(sum, "z1 + z2");

    return 0;
}
                                    

Program Output

z1 = 3.00 + 4.00i
z2 = 1.50 + 2.50i
z1 + z2 = 4.50 + 6.50i
                                

Step-by-Step Explanation

Let's look at each part of the program.

1. The struct definition:

struct Complex {
    double real;
    double imag;
};
                                

This defines a struct type that holds two double values. Its full name is struct Complex. From this point, you can already write struct Complex z; — but it's a mouthful.

2. The typedef alias:

typedef struct Complex Complex;
                                

This line says: "the name Complex now means struct Complex". It's a pure declaration — no memory, no runtime cost. From this point, Complex and struct Complex are exactly the same type.

3. Functions that take Complex:

void printComplex(Complex z, const char *label);
Complex addComplex(Complex a, Complex b);
                                

Because Complex is a normal type now, you can use it in function signatures without the struct keyword. addComplex returns a new Complex by value — perfectly safe, since it's just a 16-byte struct on most systems.

4. Declaring and using variables:

Complex z1;
z1.real = 3.0;
z1.imag = 4.0;
                                

Complex z1; is identical to struct Complex z1;. Member access works exactly as it always does — z1.real and z1.imag.

5. The alternate one-liner form:

typedef struct {
    double real;
    double imag;
} Complex;
                                

This combines the struct definition and the typedef into a single statement. The struct tag is omitted entirely, which is often cleaner. Functionally identical — pick whichever style reads better to you.

🎯 Remember this: typedef doesn't change the type — it only changes what you have to type. Use it whenever a struct name gets in the way of reading your code clearly. Your future self will thank you.

Frequently Asked Questions

1. Does typedef create a new type?

No. It creates a new name for an existing type. The compiler still treats Complex and struct Complex as identical for type-checking — you can freely assign one to the other, pass one where the other is expected, etc.

2. Is typedef a storage class?

Not really — it's a type-alias construct. It's often listed alongside storage classes because it uses the same slot in a declaration's grammar, but it has no effect on where the variable is stored or how long it lives.

3. Can I use typedef with pointers?

Yes. For example, typedef struct Node *NodePtr; creates NodePtr as an alias for "pointer to struct Node". Then NodePtr head = NULL; works cleanly. Watch out for the trap: typedef int *IntPtr; makes IntPtr a, b; declare both as pointers, whereas int *a, b; makes only a a pointer.

4. Which style should I use — tagged struct + typedef, or anonymous struct?

Use the tagged form (struct Complex { ... }; typedef struct Complex Complex;) if other code needs to refer to the struct by its struct name (e.g., forward declarations). Use the anonymous form (typedef struct { ... } Complex;) if the struct is used only through the typedef — it's shorter and hides the internal tag.

5. Does typedef take any memory or slow my program down?

No. typedef is entirely a compile-time feature. It produces no code, no data, no runtime cost. The generated machine code is byte-for-byte identical whether you use the typedef name or the full struct Complex form.

💡 Tip: Combine typedef with a self-referential struct (like a linked-list node) to make recursive data structures easier to write — typedef struct Node { int data; struct Node *next; } Node; — you'll never have to type struct Node again.

📖 Related Tutorials

  • Storage Classes in C — Full Tutorial
  • 4. extern Storage Class Across Files
  • 6. Combining Multiple Storage Classes
  • Type Defined Structure in C
  • All Storage Class Assignments

Previous Topic: -->> 4. extern Storage Class Across Files   ||   Next topic: -->> 6. Combining Multiple Storage Classes


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.