Computer Assignment
Computer Assignment
Computer Assignment
ASSIGNMENT
SUBMITTED BY-DILJOT KAUR
BSC.HONS PHYSICS
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;...};
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
} var_name;
var_name.memeber_name = value;
#include <stdio.h>
struct numbers
{
int num1, num2;
};
int main()
{
// Assignment using using designated
initialization
struct numbers s1 = {.num2 = 22, .num1 =
11};
struct numbers s2 = {.num2 = 30};
printf ("num1: %d, num2: %d\n", s1.num1,
s1.num2);
printf ("num1: %d", s2.num2);
return 0;
}
Example of Structure in C
#include <stdio.h>
Array of Structure :
Structure is used to store the information of One
particular object but if we need to store such 100
objects then Array of Structure is used.
Example
#include <stdio.h>
struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];
for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}
for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);
}
return 0;}
Eplanation
Program.
#include <stdio.h>
struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];
for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}
for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);n
}
return 0;
}
Accessing Element in Structure Array
1. Array of Structure can be accessed using dot[.]
operator.
2. Here Records of 3 Employee are Stored.
3. ‘for loop’ is used to Enter the Record of first
Employee.
4. Similarly ‘for Loop’ is used to Display Record.
Example
#include<stdio.h>
#include<conio.h>
struct Employee
{
int ssn;
char ename[20];
char dept[20];
}emp[3];
//---------------------------------------------
void main()
{
int i,sum;
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0;
}
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}