Showing posts with label enum. Show all posts
Showing posts with label enum. Show all posts

Monday, June 3, 2019

all about enums


Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

enum State {Working = 1, Failed = 0};
The keyword ‘enum’ is used to declare new enumeration types in C and C++. Following is an example of enum declaration.
// The name of enumeration is "flag" and the constant
// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and
// so on.
enum flag{constant1, constant2, constant3, ....... };
Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is
// defined as the variable of type week.

enum week{Mon, Tue, Wed};
enum week day;

// Or

enum week{Mon, Tue, Wed}day;
filter_none
edit
play_arrow
brightness_4
// An example program to demonstrate working
// of enum in C
#include<stdio.h>
 
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
 
int main()
{
    enum week day;
    day = Wed;
    printf("%d",day);
    return 0;
}
Output:


2
In the above example, we declared “day” as the variable and the value of “Wed” is allocated to day, which is 2. So as a result, 2 is printed.
Another example of enumeration is:
filter_none
edit
play_arrow
brightness_4
// Another example program to demonstrate working
// of enum in C
#include<stdio.h>
 
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, 
          Aug, Sep, Oct, Nov, Dec};
 
int main()
{
   int i;
   for (i=Jan; i<=Dec; i++)      
      printf("%d ", i);
       
   return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11
In this example, the for loop will run from i = 0 to i = 11, as initially the value of i is Jan which is 0 and the value of Dec is 11.
 

Interesting facts about initialization of enum.
1. Two enum names can have same value. For example, in the following C program both ‘Failed’ and ‘Freezed’ have same value 0.
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
 
int main()
{
   printf("%d, %d, %d", Working, Failed, Freezed);
   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, sunday gets value 0, monday gets 1, and so on.
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};
 
int main()
{
    enum day d = thursday;
    printf("The day number stored in d is %d", d);
    return 0;
}
Output:
The day number stored in d is 4


3. We can assign values to some name in any order. All unassigned names get value as value of previous name plus one.


filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 5,
          wednesday, thursday = 10, friday, saturday};
 
int main()
{
    printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
            wednesday, thursday, friday, saturday);
    return 0;
}
Output:
1 2 5 6 10 11 12


4. The value assigned to enum names must be some integeral constant, i.e., the value must be in range from minimum possible integer value to maximum possible integer value.


5. All enum constants must be unique in their scope. For example, the following program fails in compilation.
filter_none
edit
play_arrow
brightness_4
enum state  {working, failed};
enum result {failed, passed};
 
int main()  { return 0; }
Output:
Compile Error: 'failed' has a previous declaration as 'state failed'


Exercise:
Predict the output of following C programs
Program 1:
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
enum day {sunday = 1, tuesday, wednesday, thursday, friday, saturday};
 
int main()
{
    enum day d = thursday;
    printf("The day number stored in d is %d", d);
    return 0;
}


Program 2:
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
enum State {WORKING = 0, FAILED, FREEZED};
enum State currState = 2;
 
enum State FindState() {
    return currState;
}
 
int main() {
   (FindState() == WORKING)? printf("WORKING"): printf("NOT WORKING");
   return 0;
}


Enum vs Macro
We can also use macros to define names constants. For example we can define ‘Working’ and ‘Failed’ using following macro.
filter_none


brightness_4
#define Working 0
#define Failed 1
#define Freezed 2
There are multiple advantages of using enum over macro when many related named constants have integral values.
a) Enums follow scope rules.
b) Enum variables are automatically assigned values. Following is simpler
filter_none


brightness_4
enum state  {Working, Failed, Freezed};

Ref:
https://www.geeksforgeeks.org/enumeration-enum-c/
https://www.geeksforgeeks.org/static-const-vs-define-vs-enum/

typdefs for pointer and function pointer and enum and struct

typedef in function pointer:




int add(int a, int b)
{
    return (a+b);
}

typedef int (*add_integer)(int, int); //declaration of function pointer

int main()
{
    add_integer addition = add; //typedef assigns a new variable i.e. 
                                //"addition" to original function "add"
    int c = addition(11, 11);   //calling function via new variable
    printf("%d",c);
    return 0;
}



Ref: https://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c


typedef in pointer:


The typedef may be used in declaration of a number of pointers of the same type. It does not change the type, it only creates a synonym, i.e., another name for the same type as illustrated below.
typedef float* FP; // Now FP represents float*
float x,y,z;
FP px =&x, py = &y, pz = &z ; // Use of FP
In the above code px, py, and pz are pointers initialized with addresses of x, y, and z respectively.
Illustrates use of typedef in pointer declaration.


#include <stdio.h>
void main()
{
   typedef double* Dp;
   double x = 10.5,y = 8.6, z = 12.5;
   Dp px = &x , py = &y, pz = &z;
   clrscr();
   printf("x = %.3lf,\t *px =  %.3lf\t px = %p \n", x, *px, px);
   printf("y = %.3lf,\t *py =  %.3lf\t py = %p \n", y, *py, py);
   printf("z = %.3lf,\t *pz =  %.3lf\t pz = %p \n", z, *pz, pz);
} 


Reference from:
http://ecomputernotes.com/what-is-c/function-a-pointer/typedef-in-pointer

typedef in enum:




way 1: 
enum State {Working = 1, Failed = 0}; 
way 2: 
typedef enum {false, true} boolean;
boolean error = false;
         


Reference from:
http://ecomputernotes.com/what-is-c/function-a-pointer/typedef-in-pointer

typedef in struct:



#include <stdio.h>
#include <string.h>
 
typedef struct Books {
   char title[50];
   char author[50];
   char subject[100];
   int book_id;
} Book;
 
int main( ) {

   Book book;
 
   strcpy( book.title, "C Programming");
   strcpy( book.author, "Nuha Ali"); 
   strcpy( book.subject, "C Programming Tutorial");
   book.book_id = 6495407;
 
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);

   return 0;
}


Ref: https://www.tutorialspoint.com/cprogramming/c_typedef.htm