Saturday, May 21, 2011

C program to explain use of Structures

/*Code a program to do the following with a structure:
a. Declare the structure with the following members:
Name char 20
Age int
Gender char 1
Marks float
b. Initialize the members with your values.
c. Print the memory space occupied by the structure object.
d. Print the formatted structure data.*/

int main()
{
int n,i;
struct marks
{
char name[20];
int age;
char gen;
float marks;
}a[50];


printf("\nEnter the no of entries");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the name");
scanf("%s",&a[i].name);
printf("\nEnter the age");
scanf("%d",&a[i].age);
printf("\nEnter the gender");
scanf("%c",&a[i].gen);
printf("\nEnter the no of marks");
scanf("%f",&a[i].marks);
}
for(i=0;i {
printf("size of object a%d is %d",i,sizeof(a[i]));
}
for(i=0;i {
printf("\nname %s",a[i].name);
printf("\nage %d",a[i].age);
printf("\n%c",a[i].gen);
printf("\n%f",a[i].marks);
}
system("pause");
return 0;
}

No comments:

Post a Comment

Google+ Followers