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
typedefsfor Basic data types can help to improve the readability of the code and make it easier to understand. - For example: using
UnsignedIntinstead ofuintmakes it clear that the data type represents anunsigned integer. Similarly, usingStringinstead ofstrmakes 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, andString. - 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
printfstatements.
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
Datethat represents a date with aday,month, andyear. - We then use
typedefto create an alias for this struct type calledDate. - In the
mainfunction, we create a variable of typeDatecalleddand initialize it with the values { 21, 2, 2023 }. - We then print out the values of
d.day,d.month, andd.yearusingprintf.
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
Daythat represents the days of the week. - We then use
typedefto create an alias for this enumerated data type calledDay. - In the
mainfunction, we create a variable of typeDaycalledtodayand initialize it with the valueTUESDAY. - We then print out the name of the day using a
switchstatement that checks the value oftoday.
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
typedefto create an alias for apointer 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 = #
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
typedefcalledDataPtrthat is an alias for a pointer tovoid. - In the
mainfunction, we create a variable of typeintcallednumand initialize it with the value 42, and a variable of typecharcalledcand initialize it with the value 'A'. - We then create a variable of type
DataPtrcalledptrand use it to point to the address ofnum. - We print out the value of
numby dereferencingptrand casting it to a pointer toint. - We then set
ptrto point to the address ofc, and print out the value ofcby dereferencingptrand casting it to a pointer tochar.