typedef and enum in C





typedef ?

  • The C programming language provides a keyword called typedef, which you can use to give a type, a new name.

  • Typedef can be used to simplify the real commands as per our need.

Syntax :

typedef data_type new_name;

Example :

#include<stdio.h>
#include<stdlib.h>
typedef char* string; 
int main()
{
 string name; 
 name = malloc(256);
 printf("Enter Name: ");
 scanf("%s",name);
 printf("--------\n");
 printf("Hello %s !!",name);
 return 0;
}
Output :
Enter Name: Haziq
Hello Haziq !!

Enumeration ( enum ) ?

  • Enum is user defined data type like structure,union.

  • Enum is used to define a set of constants of type int.

  • To declare enum , we use keyword enum.

Syntax :

enum tag{enumeration list};
e.g.
enum day { MON, TUE, WED, THU, FRI, SAT, SUN };

Here name of enumeration is day.

And, MON, TUE, WED,.... are the values of type day.

By default, MON is 0, TUE is 1 and so on. This is done by compiler and it starts from 0, You can change default values of enum elements during declaration (if necessary).

Example :

#include<stdio.h>
int main()
{
 int i;
 enum day {MON,TUE,WED,THU,FRI,SAT,SUN};
  for(i=MON;i<=SUN;i++) {
      printf("%d ",i);
  }
return 0;
}
Output
0 1 2 3 4 5 6

Points to remember :

  1. Two enum names can have same value. For example,

    #include<stdio.h>
    enum exam {Pass = 1, Failed = 0, Awaited = 0};
     
    int main()
    {
       printf("%d, %d, %d", Pass, Failed, Awaited);
       return 0;
    }  
    Output
    1, 0, 0
  2. If we do not explicitly assign values to enum names, the compiler by default assigns values starting from 0. For example, in the following C program, MON gets value 0, TUE gets 1, and so on.

    #include<stdio.h>
    int main()
    {
     int i;
     enum day {MON,TUE,WED,THU,FRI,SAT,SUN};
      for(i=MON;i<=SUN;i++) {
          printf("%d ",i);
      }
    return 0;
    }
    Output
    0 1 2 3 4 5 6
  3. We can assign values to some name in any order. All unassigned names get value as value of previous name plus one.

    #include<stdio.h>
    int main()
    {
     enum day {MON = 1,TUE,WED,THU = 7,FRI,SAT,SUN};
       printf("%d %d %d %d %d %d %d ",MON,TUE,WED,THU,FRI,SAT,SUN);
    return 0;
    }
    Output
    1 2 3 7 8 9
  4. All enum constants must be unique in their scope. For example, the following program fails in compilation.

    #include<stdio.h>
    enum e1  {a, b};
    enum e2  {a, b};
    int main ( )
     {
      return 0;
     }
    Output
    Compilation Error

Next topic is dynamic memory allocation





 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved