• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Variables » typedef in c
Next →
← Prev

typedef in c

By Dinesh Thakur

The typedef feature allows us to give an alternative (possibly short and more meaningful) name to an existing data type and improve program readability. For example, instead of using the int data type to declare variables to represent marks in three subjects, we can associate a more meaningful name (say Marks)for the int data type using typedef as:

typedef int Marks;

Now we can declare variables using the new type name Marks as shown below:

Marks phy, chem, math;

It should be understood that Marks is just another name for the int data type and it does not restrict the values of variables phy,chem and math to the desired range, say 0 to 100. Also, we can perform all the operations on these variables that we can perform on variables of type int including the use of the %d format in the scanf and printf statements.

The typedef can be used to give convenient short names to buit-in data types, as shown below.

typedef unsigned short int UShort;

typedef long double LDouble;

typedef const unsigned long int ULongConst;

These typedefs declare ushort, LDouble and ULongConst as convenient shorthands for unsigned short int, long double and const unsigned long int, respectively. Note that while declaring variables using type name ULongConst, we must initialize them; otherwise, the compiler will report an error. Also note that we have used capitalized names for typedef names as a convention.

We can use other features of the C language in conjunction with the new type names. For example, we can define const variables, arrays and pointers using type name UShort as

const UShort a = 1234;

UShort b[10];

UShort *pa = &a;

The typedef is a powerful mechanism that allows us to declare new names for complex types that involve arrays, pointers, functions and their combinations. The syntax for such declarations can be quite confusing in the beginning. However, it is not very difficult if we use the following rule: To declare a new type name for a particular type, first declare this name as if we were declaring a variable of that type and then precede that declaration with the typedef keyword.

For example, to declare UShort as a new type name for unsigned short int, first declare UShort; as if it is a variable of that type as

unsigned short int UShort;

and then precede this declaration with the typedef keyword as shown below.

typedef unsigned short int UShort;

Finally note that the typedefs are generally written at the beginning of a program, usually after the #include statements. This allows their use in any of the structures, enums and functions including the main function. We can also write them in any function, in which case, their use is restricted to that function only.

We’ll be covering the following topics in this tutorial:

  • Using typedef with Structures
  • Using typedef with Enumerated Types

Using typedef with Structures

Recall that we have to use the struct keyword every time we declare a structure variable or parameter. This inconvenience can be avoided using typedef. For example, consider the typedef given below.

typedef struct complex {

float re, im;

) Complex;

This code declares Complex as a new type name for struct complex. Note that as per our convention, the type name is in capitalized case (complex),whereas the structure name (complex) is in lowercase letters. Also, observe how our rule simplifies writing such declarations: the new type name Complex is first written as if it is a variable of type struct complex and then the declaration is preceded by the typedef keyword.

If you prefer, the declarations of structure and typedef can be written separately as

struct complex {

float re, im;

} ;

typedef struct complex Complex;

Observe that our rule for writing a typedef is applicable in this case as well.

Once the new type name Complex is declared using either of the approaches given above, we can declare variables using this new type name (as well as by using struct complex)as shown below.

Complex a = {1,2};

struct complex b, c;

Now we can simplify the definition of the complex_add function using this new type name and greatly improve its readability, as shown below.

/* addition of two complex numbers */

Complex complex_add(Complex a, Complex b)

{

  Complex c;

  c.re = a.re + b.re;

  c.im = a.im + b.im;

  return c;

}

Note that as discussed earlier, we can continue to use other features of the C language in conjunction with a new type name. For example, we can declare arrays and pointers of this type as

Complex a[10];

Complex *pa;

Finally note that the typedef name can be the same as that of the structure name. For example,

typedef struct Complex {

float re, im;

} Complex;

Also, we can even omit the structure name as shown below.

typedef struct {

float re, im;

) Complex;

Using typedef with Enumerated Types

Thus, we can declare a new type name for enumerated types using typedef as

typedef enum color {Violet, Indigo, Blue, Green, Yellow, Orange, Red} RbColor;

or if you prefer, we can separate these declarations as

enum color {Violet, Indigo, Blue, Green, Yellow, Orange, Red};

typedef enum color RbColor;

These declarations define RbColor as a new type name for enumcolor. We can use this new type name wherever we can use enumcolor. Thus, we can define various entities using this new type name, such as variables, arrays, pointers, function parameters, structure members, etc. In addition, we can also use enumcolor for this purpose, although it is unnecessary now. For example, we can declare variables as shown below.

RbColor clr = Blue;

enum Color cl = Violet, c2 = Red;

Note that the typedef name can be the same as that of the enumerated type name and we can also omit the enumerated type name, as in case of typedefs for structures.

Illustrates application of typedef.

#include <stdio.h> 
 int main() 
 {  
    typedef int I; 
    typedef double dbl; 
    I a =2,b =6; 
    dbl K, S, T = 3.0; 
    clrscr(); 
    K = b/T; 
    S = T/ a; 
    printf(" K = %f\t s = %f\n", K,S); 
    return 0; 
 } 

You’ll also like:

  1. What do you mean by typedef
  2. C Program to use typedef in pointer
  3. Typedef in Structure Declaration in C
  4. Typedef with Nested Structures in C
  5. typedef for Pointers to Functions in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW