Professional Documents
Culture Documents
Array Structure
Structure Union
Example of structure: Program to input student name, rollno and marks of math, nepali and
computer then calculate total mark, percentage using structure variable.
#include<stdio.h>
#include<conio.h>
struct ram
{
char stuname[20];
int rollno;
float math,nep,com,tot,per;
};
void main( )
{
struct ram a;
printf(“Enter student name”);
gets(a.stuname);
printf(“Enter roll number”);
scanf(“%d”,&a.rollno);
printf(“Enter marks of math nepali and computer”);
scanf(“%f %f %f”, &a.math, &a.nep, &a.com);
a.tot=a.math+a.nep+a.com;
a.per=a.tot/3;
printf(“Your total number is %f”,a.tot);
printf(“percentage is %f”,a.per);
getch( );
}
Example of union: Program to input student name, rollno and marks of math, nepali and computer
then calculate total mark, percentage using union variable.
#include<stdio.h>
#include<conio.h>
union ram
{
char stuname[20];
int rollno;
float math, nep, com, tot, per;
};
void main( )
{
union ram a;
printf(“Enter student name”);
gets(a.stuname);
printf(“Enter roll number”);
scanf(“%d”,&a.rollno);
printf(“Enter number of math nepali and computer”);
scanf(“%f %f %f”, &a.math, &a.nep, &a.com);
a.tot=a.math+a.nep+a.com;
a.per=a.tot/3;
printf(“Your total number is %f”, a.tot);
printf(“percentage is %f”, a.per);
getch( );
}
Function Description
Example: Program to read records from data file student.txt and delete the record if its name
match with name "Ramesh".
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
clrscr( );
FILE *f1,*f2;
char sname[20];
int i;
//code to delete the name that matches with name Ramesh
f1=fopen("student.txt","r");
f2=fopen("newrecord.txt","w");
while(fscanf(f1, "%s", name)==1)
{
printf("%s\n",sname);
if(strcmp(sname,"Ramesh")==0)
{
continue;
}
fprintf(f2, "%s", sname);
}
fclose(f1);
fclose(f2);
remove("studen.txt");
rename("newrecord.txt","student.txt");
getch( );
}