C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

struct s

{

char name[50];

int height;

};

int main(){

struct s a[5],b[5];

FILE *fptr;

int i;

fptr=fopen("file.txt","wb");

for(i=0;i<5;++i)

{

fflush(stdin);

printf("Enter name: ");

gets(a[i].name);

printf("Enter height: ");

scanf("%d",&a[i].height);

}

fwrite(a,sizeof(a),1,fptr);

fclose(fptr);

fptr=fopen("file.txt","rb");

fread(b,sizeof(b),1,fptr);

for(i=0;i<5;++i)

{

printf("Name: %s\nHeight: %d",b[i].name,b[i].height);

}

fclose(fptr);

}

C Programming Enumeration

Enumeration type allows programmer to define their own data type . Keyword enum is used to defined enumerated data type.

enum type_name{ value1, value2,...,valueN };

Here, type_name is the name of enumerated data type or tag. And value1, value2,....,valueN are values of typetype_name.

By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value as below: enum suit{

club=0;

diamonds=10;

hearts=20;

spades=3;

};

Declaration of enumerated variable

Above code defines the type of the data but, no any variable is created. Variable of type enum can be created as:

enum boolean{

false;

true;

};

enum boolean check;

Here, a variable check is declared which is of type enum boolean.

Example of enumerated type

#include <stdio.h>

enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main(){

enum week today;

today=wednesday;

printf("%d day",today+1);

return 0;

}