Write a C program to demonstrate Enumerations, Structures and Union data types.
Using structure #include<stdio.h> struct student
{
float p,c,m;
};
int main()
{
int n,i;
struct student s1[100];
printf("\n enter number of student"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the marks for physics="); scanf("%f",&s1[i].p);
printf("\n enter the marks for chemistry="); scanf("%f",&s1[i].c);
printf("\n enter the marks for maths="); scanf("%f",&s1[i].m);
}
printf("\n marks are"); for(i=1;i<=n;i++)
printf("%f\n%f\n%f\n",s1[i].p,s1[i].c,s1[i].m); return 0;
}
Using UNION #include<stdio.h> #include<string.h> union book
{
int a;
char name ;
//char author[20]; float price;
};
int main()
{
union book b1;
printf("\n enter the integer value"); scanf("%d",&b1.a);
printf("\n%d",b1.a);
printf("\n enter the name of book"); scanf("%c",&b1.name);
printf(" %c",b1.name); printf("\n enter the price"); scanf(" %f",&b1.price); printf("\n %f",b1.price);
return 0;
}
Write a C program to demonstrate the input and output operations on files.
#include<stdio.h> int n;
struct student
{
int usn;
char name[10]; int sem;
};
int main()
{
struct student s1[n]; FILE *fp;
fp=fopen("student.txt","w"); int i;
printf("\n enter the number of student"); scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the details"); scanf("%d%s%d",&s1[i].usn,s1[i].name,&s1[i].sem);
fprintf(fp,"%d%s%d",s1[i].usn,s1[i].name,s1[i].sem);
}
fclose(fp); fp=fopen("student.txt","r"); for(i=0;i<n;i++)
{
fscanf(fp,"%d%s%d",&s1[i].usn,s1[i].name,&s1[i].sem);
printf("%d%s%d",s1[i].usn,s1[i].name,s1[i].sem);
}
fclose(fp); return 0;
}