Skip to main content

Defining Typedef in C

Defining Typedef

Typedefs can be defined for several types of data types, as described below:

Typedef for Basic data types

Basic data types can be used to represent simple values like integers, floating-point numbers, characters, and no value respectively.

Syntax

Here is an syntax example of defining typedef for basic data types:

typedef unsigned int UnsignedInt;
typedef signed char SignedChar;
typedef short int ShortInt;
typedef long int LongInt;
typedef long long int LongLongInt;
typedef float Float;
typedef double Double;
typedef long double LongDouble;
typedef char* String;
  • Using typedefs for Basic data types can help to improve the readability of the code and make it easier to understand.
  • For example: using UnsignedInt instead of uint makes it clear that the data type represents an unsigned integer. Similarly, using String instead of str makes it clear that the data type represents a string.

EXample

#include <stdio.h>

typedef unsigned int UnsignedInt;
typedef signed char SignedChar;
typedef short int ShortInt;
typedef long int LongInt;
typedef long long int LongLongInt;
typedef float Float;
typedef double Double;
typedef long double LongDouble;
typedef char *String;

int main()
{
UnsignedInt ui = 123;
SignedChar sc = 'a';
ShortInt si = 32767;
LongInt li = 2147483647;
LongLongInt lli = 9223372036854775807;
Float f = 3.14159;
Double d = 3.141592653589793;
LongDouble ld = 3.14159265358979323846L;
String str = "Hello, world!";

printf("UnsignedInt: %u\n", ui);
printf("SignedChar: %c\n", sc);
printf("ShortInt: %d\n", si);
printf("LongInt: %ld\n", li);
printf("LongLongInt: %lld\n", lli);
printf("Float: %f\n", f);
printf("Double: %lf\n", d);
printf("LongDouble: %Lf\n", ld);
printf("String: %s\n", str);

return 0;
}
Output:

Explanation:

  • We use typedef to create aliases for several basic data types, including UnsignedInt, SignedChar, ShortInt, LongInt, LongLongInt, Float, Double, LongDouble, and String.
  • We then declare variables of each of these types and initialize them with some sample values.
  • Finally, we print out the values of these variables using printf statements.
note

By using the aliases we defined with typedef, the code is more readable and easier to understand.

Typedef for Derived data types

Derived data types are data types that are derived from the basic data types and include pointers, arrays, and structures.

TypeDef for a struct type

#include <stdio.h>

typedef struct
{
int day;
int month;
int year;
} Date;

int main()
{
Date d = {21, 2, 2023};
printf("The date is: %d/%d/%d\n", d.day, d.month, d.year);
return 0;
}
Output:

Explanation:

  • We define a struct type called Date that represents a date with a day, month, and year.
  • We then use typedef to create an alias for this struct type called Date.
  • In the main function, we create a variable of type Date called d and initialize it with the values { 21, 2, 2023 }.
  • We then print out the values of d.day, d.month, and d.year using printf.

Typedef for Enumerated data types

Enumerated data types are user-defined data types that allow you to define a set of named constants, such as RED, GREEN, and BLUE, that represent different values of a variable.

Example

#include <stdio.h>

typedef enum
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Day;

int main()
{
Day today = TUESDAY;
printf("Today is ");
switch (today)
{
case MONDAY:
printf("Monday");
break;
case TUESDAY:
printf("Tuesday");
break;
case WEDNESDAY:
printf("Wednesday");
break;
case THURSDAY:
printf("Thursday");
break;
case FRIDAY:
printf("Friday");
break;
case SATURDAY:
printf("Saturday");
break;
case SUNDAY:
printf("Sunday");
break;
}
printf("\n");
return 0;
}
Output:

Explanation:

  • We define an enumerated data type called Day that represents the days of the week.
  • We then use typedef to create an alias for this enumerated data type called Day.
  • In the main function, we create a variable of type Day called today and initialize it with the value TUESDAY.
  • We then print out the name of the day using a switch statement that checks the value of today.

Typedef for Void data type

  • Since void does not have a specific size or representation, it cannot be used as the base type for a typedef.
  • However, you can use typedef to create an alias for a pointer to void, which can be used to represent a pointer to any type.

Example

#include <stdio.h>

typedef void *DataPtr;

int main()
{
int num = 42;
char c = 'A';
DataPtr ptr;

ptr = &num;
printf("The value of num is %d\n", *(int *)ptr);

ptr = &c;
printf("The value of c is %c\n", *(char *)ptr);

return 0;
}
Output:

Explanation:

  • We define a typedef called DataPtr that is an alias for a pointer to void.
  • In the main function, we create a variable of type int called num and initialize it with the value 42, and a variable of type char called c and initialize it with the value 'A'.
  • We then create a variable of type DataPtr called ptr and use it to point to the address of num.
  • We print out the value of num by dereferencing ptr and casting it to a pointer to int.
  • We then set ptr to point to the address of c, and print out the value of c by dereferencing ptr and casting it to a pointer to char.