Sie sind auf Seite 1von 5

enum in C

Enumeration (enum) is a user-defined datatype (same as structure). It consists of


various elements of that type. There is no such specific use of enum, we use it just to
make our codes neat and more readable. We can write C programs without using
enumerations also.

For example, Summer, Spring, Winter and Autumn are the names of four seasons.
Thus, we can say that these are of types season. Therefore, this becomes an
enumeration with name season and Summer, Spring, Winter and Autumn as its
elements.

So, you are clear with the basic idea of enum. Now let's see how to define it.

Defining an Enum

An enum is defined in the same way as structure with the keyword struct replaced


by the keyword enum and the elements separated by 'comma' as follows.

enum enum_name
{
  element1,
  element2,
  element3,
  element4,
};

Now let's define an enum of the above example of seasons.

enum Season{
  Summer,
  Spring,
  Winter,
  Autumn
};

Here, we have defined an enum with name 'season' and 'Summer, Spring, Winter
and Autumn' as its elements.

Declaration of Enum Variable

We also declare an enum variable in the same way as that of structures. We create
an enum variable as follows.

enum season{
  Summer,
  Spring,
  Winter,
  Autumn
};
main()
{
  enum season s;
}

So, here 's' is the variable of the enum named season. This variable will represent a
season. We can also declare an enum variable as follows.

enum season{
  Summer,
  Spring,
  Winter,
  Autumn
}s;

Values of the Members of Enum


All the elements of an enum have a value. By default, the value of the first element is
0, that of the second element is 1 and so on.

C Program to Search an Array Element


using LINEAR SEARCH
#include <stdio.h>
void main()
{
    int a[10], i, item;
    printf("\nEnter SEVEN elements of an array:\n");
    for (i=0; i<=6; i++)
        scanf("%d", &a[i]);
    printf("\nEnter item to search: ");
    scanf("%d", &item);
    for (i=0; i<=9; i++)
        if (item == a[i])
        {
            printf("\nItem found at location %d", i+1);
            break;
        }
    if (i > 9)
        printf("\nItem does not exist.");
    getch();
}

Count the number of words and characters


in a file
#include <stdio.h>

#include <stdlib.h>

void main()

FILE *fptr;

char ch;

int wrd=1,charctr=1;

char fname[20];

printf("\n\n Count the number of words and characters in a file :\n");


printf("---------------------------------------------------------\n");

printf(" Input the filename to be opened : ");

scanf("%s",fname);

fptr=fopen(fname,"r");

if(fptr==NULL)

printf(" File does not exist or can not be opened.");

else

ch=fgetc(fptr);

printf(" The content of the file %s are : ",fname);

while(ch!=EOF)

printf("%c",ch);

if(ch==' '||ch=='\n')

wrd++;

else

charctr++;

ch=fgetc(fptr);

printf("\n The number of words in the file %s are : %d\n",fname,wrd-2);

printf(" The number of characters in the file %s are :


%d\n\n",fname,charctr-1);

}
fclose(fptr);

Program : Copy Text From One File to Other File


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
void main() {
   FILE *fp1, *fp2;
   char ch;
   clrscr();
 
   fp1 = fopen("Sample.txt", "r");
   fp2 = fopen("Output.txt", "w");
 
   while (1) {
      ch = fgetc(fp1);
 
      if (ch == EOF)
         break;
      else
         putc(ch, fp2);
   }
 
   printf("File copied Successfully!");
   fclose(fp1);
   fclose(fp2);
}

Das könnte Ihnen auch gefallen