Friday, July 29, 2016

C Basics - Enumeration in C

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.

The general form is,

enum identifier { enumerator-list }

The optional identifier names the enumeration type defined by enumerator-list.

Enumerations provide an alternative to the #define preprocessor directive with the advantages that the values can be generated for you and obey normal scoping rules.

The expressions that define the value of an enumerator constant always have int type. A variable with enumeration type stores one of the values of the enumeration set defined by that type.

An enumeration set can contain duplicate constant values. The identifiers in the enumeration list must be distinct from other identifiers.

#include <stdio.h>

enum bool {
} ;
int main( ) {

   enum bool flag; 
   int x = 0, y = 0,z = 0; 

   printf("\nx = %d, Y = %d",x,y);
   z = x | y;
   if(z == true){
    printf( "\nx | y = %d is TRUE", z);
    printf( "\nx | y = %d is FALSE", z);
 z = x & y;
   if(z == true){
    printf( "\nx & y = %d is TRUE", z);
    printf( "\nx & y = %d is FALSE", z);

   return 0;
The output of the above program would be:

x = 1, Y = 0
x | y = 1 is TRUE
x & y = 0 is FALSE

Related topics:
Structures in C   |   Structures and Function Parameters in C   |   Structures and Pointers in C   |   Structure Bit Fields in C   |   Union in C   |   Typedef in C

List of topics: C Programming

No comments:

Post a Comment