Typedef in C

  • A typedef in C declaration lets you define your own identifiers that can be used in place of type specifiers such as int, float, and double.
  • A typedef declaration does not reserve storage.
  • The names you define using typedef are not new data types, but synonyms for the data types or combinations of data types they represent.
  • The following statements define LENGTH as a synonym for int and then use this typedef to declare length, width, and height as integer variables:
    typedef int LENGTH;
    LENGTH length, width, height;

Typedef in c Typedef in c Typedef in c Typedef in c Typedef in c Typedef in c Typedef in c Typedef in c

The following declarations are equivalent to the above declaration:
int length, width, height

Some more examples of Typedef in C

typedef int aaa, bbb, ccc;
typedef int ar[15], arr[9][6];
typedef char c, *cp, carr[100];

  •  User-defined identifiers are not permanent data types, these are working like synonyms of pre-defined types.
  • These identifiers can be global or local.

#include
main()
{
typedef int my_int ;
my_int a=10,b=20,c;
c = a+b;
printf(“res : %d\n”,c);
}
#include
main()
{
typedef int my_arr[5];
int i;
my_arr a = {10,20,30,40,50};
printf(“Array elements are\n”);
for(i=0 ; i<5 ; i++)
{
printf(“%d\n”,a[i]);
}
}

#include
typedef char* String;
String read(void);
main()
{
String name;
name = read();
printf(“welcome %s\n”,name);
}
String read()
{
String name;
printf(“Enter one name : “);
gets(name);
return name;
}
#include
typedef struct emp
{
int eno;
char* ename;
float esal;
}Employee;
main()
{
//typedef struct emp Employee;
Employee e1;
}
#include
struct emp
{
int eno;
char* ename;
float esal;
};
main()
{
typedef struct emp Employee;
typedef struct emp* Eptr;
Eptr e;
e = (Eptr)malloc(sizeof(Employee));
}

Enumeration
  • An enumeration consists of a set of named integer constants.
  • An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the “enumeration set,” “enumerator constants,” “enumerators,” or “members”).
  • A variable with enumeration type stores one of the values of the enumeration set defined by that type.
  • An enumeration constant or a value of enumerated type can be used anywhere the C language permits an integer expression.

Syntax: enum identifier { enumerator-list }

  • Each enumeration-constant in an enumeration-list names a value of the enumeration set. By default, the first enumeration-constant is associated with the value 0. The next enumeration constant in the list is associated with the value of ( constant-expression + 1 ), unless you explicitly associate it with another value. The name of an enumeration-constant is equivalent to its value.
  • You can use enumeration-constant = constant-expression to override the default sequence of values. Thus, if enumeration-constant = constant-expression appears in the enumeratorlist, the enumeration-constant is associated with the value given by constant-expression. The constant-expression must have int type and can be negative.

The following rules apply to the members of an enumeration set:

  • An enumeration set can contain duplicate constant values. For example, you could associate the value 0 with two different identifiers, perhaps named null and zero, in the same set.
  • The identifiers in the enumeration list must be distinct from other identifiers in the same scope with the same visibility, including ordinary variable names and identifiers in other enumeration lists.
  • Enumeration tags obey the normal scoping rules. They must be distinct from other enumeration, structure, and union tags with the same visibility.

#include <stdio.h>
int main()
{
enum {WIN=0, TIE=1, BYE=1, LOSE=0, NO_SHOW=0} result;
enum {SUN, MON, TUE, WED, THU, FRI, SAT} days;
result = WIN;
printf(” WIN = %d\n”, result);
result = LOSE;
printf(” LOSE = %d\n”, result);
result = TIE;
printf(” TIE = %d\n”, result);
result = BYE;
printf(” BYE = %d\n”, result);
result = NO_SHOW;
printf(“NO_SHOW = %d\n\n”, result);
for(days = MON ; days < FRI ; days++)
printf(“The day code is %d\n”, days);
return 0;
}

Code2:
#include<stdio.h>
int main()
{
enum Day {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
enum Day today;
int x;
printf(“please enter the day of the week(0 to 6)\n”);
scanf(“%d”,&x);
today=x;
if(today==Sunday || today==Saturday)
printf(“Enjoy! Its the weekend\n”);
else
printf(“Week day.do your work\n”);
return 0;
}

Typedef in c
Scroll to top