C Program For Structure and Union
Top Structure and Union C Program
C Program for Structure and Union
C Program for Structure
C program to accessing members of the structure (Operations on individual
members)-Method-1
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct std
{
int roll_no;
char name[10];
}student={4,"Ram"};
int main()
{
printf("Roll number=%d\n", student .roll_no);
printf("student name=%s", student .name);
return 0;
}
C program to accessing members of the structure (Operations on
individual members)-Method-2
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct std
{
int roll_no;
char name[10];
};
int main()
{
struct std student ={4,"Ram"};
printf("Roll number=%d\n", student .roll_no);
printf("student name=%s", student .name);
return 0;
}
C program to accessing members of the structure (Operations on
individual members)-Method-3
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct student
{
int roll_no;
char name[10];
};
int main()
{
struct student std;
printf("Enter a Roll Number");
scanf("%d",&std.roll_no);
printf("Enter name");
scanf("%s",&std.name);
printf("Roll number=%d\n", std.roll_no);
printf("student name=%s", std.name);
return 0;
}
C Program to print sum of
two number using structure (
Operations on Structures through individual members )-Method-1
#include <stdio.h>
#include <stdlib.h>
struct add
{
int foot;
int inch;
} sum={4,8};
int main()
{
int sum1;
sum1=sum.foot+sum.inch;
printf("addition of two
number=%d", sum1);
return 0;
}
C Program to print sum of
two number using structure (
Operations on Structures through individual members )-Method-2
#include <stdio.h>
#include <stdlib.h>
struct add
{
int foot;
int inch;
};
struct add sum={4,8};
int main()
{
int sum1;
sum1=sum.foot+sum.inch;
printf("addition of two
number=%d", sum1);
return 0;
}
C Program to print sum of
two number using structure (Operations on Structures
through individual members )-Method-3
#include <stdio.h>
#include <stdlib.h>
struct add
{
int foot;
int inch;
};
int main()
{
int sum1;
struct add sum={4,8};
sum1=sum.foot+sum.inch;
printf("addition of two number=%d", sum1);
return 0;
}
C Program to print sum of
two number using structure (
Operations on Structures through individual members )-Method-4
#include <stdio.h>
#include <stdlib.h>
struct add
{
int foot;
int inch;
};
int main()
{
int sum1;
struct add sum;
printf("Enter feet: ");
scanf("%d", &sum.foot);
printf("Enter inch: ");
scanf("%d", &sum.inch);
sum1=sum.foot+sum.inch;
printf("addition of two
number=%d", sum1);
return 0;
}
C Program to implement of
array using structure-Method-1
#include<stdio.h>
#include<conio.h>
struct Point
{
int x, y;
};
void main()
{
struct Point array[15];
array[0].x = 10;
array[0].y = 20;
printf("%d %d", array[0].x, array[0].y);
getch();
}
C Program to implement of
array using structure-Method-2
#include<stdio.h>
#include<conio.h>
#include <string.h>
struct student
{
int roll_no;
char name[10];
};
int main()
{
int i;
struct student std[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&std[i].roll_no);
printf("\nEnter Name:");
scanf("%s",&std[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
printf("\nRollno:%d, Name:%s",std[i].rollno,std[i].name);
}
return 0;
}
C Program to implement of
array using structure for book keeping record-Method-1
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct BookRecord
{
char book_name[25];
int pages;
char author[25];
float price;
}b1[4] = {
{"Book1",700,"YPK“,250.00},
"Book2",500,"AAK",350.00},
{"Book3",120,"HST",450.00}
{"Book3",120,"HST",450.00}
};
void main()
{
printf("\nBook Name : %s",b1[0].bname);
printf("\nBook Pages : %d",b1[0].pages);
printf("\nBook Author : %s",b1[0].author);
printf("\nBook Price : %f",b1[0].price);
printf("\nBook Name : %s",b1[1].bname);
printf("\nBook Pages : %d",b1[1].pages);
printf("\nBook Author : %s",b1[1].author);
printf("\nBook Price : %f",b1[1].price);
printf("\nBook Name : %s",b1[2].bname);
printf("\nBook Pages : %d",b1[2].pages);
printf("\nBook Author : %s",b1[2].author);
printf("\nBook Price : %f",b1[2].price);
getch();
}
C Program to implement of
array using structure for book keeping record-Method-2
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct BookRecord
{
char[25] b_name;
int pages;
int price;
} book[3];
int main(int argc, char *argv[])
{
int i;
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);
}
printf("\n---Book Details -----
");
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;
}
C Program to implement of
array using structure for student information record-Method-1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct student
{
char name[25];
int std_id;
float marks;
};
void main()
{
struct student s1,s2,s3;
printf("Enter the name, id, and marks
of student 1 ");
scanf("%s %d
%f",s1.name,&s1.std_id, &s1.marks);
printf("Enter the name, id, and marks
of student 2 ");
scanf("%s %d
%f",s2.name,&s2.std_id,&s2.marks);
printf("Enter
the name, id, and marks of student 3 ");
scanf("%s %d
%f",s3.name,&s3.std_id,&s3.marks);
printf("Printing the details....\n");
printf("%s %d
%f\n",s1.name,s1.std_id,s1.marks);
printf("%s %d
%f\n",s2.name,s2.std_id,s2.marks);
printf("%s %d
%f\n",s3.name,s3.std_id,s3.marks);
getch();
}
C Program to implement of structure using function.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct student
{
char name[30];
int rollno;
int marks;
};
void display(char name[],
int rollno, int marks);
int main()
{
struct student std =
{"Ram", 1, 88};
display(std.name, std.rollno,
std.marks);
return 0;
}
void display(char name[],
int rollno, int marks)
{
printf("Name:
%s\n", name);
printf("Roll no:
%d\n", rollno);
printf("Marks:
%d\n", marks);
printf("\n");
}
C Program to access structure members
using pointer-Method-1
#include <stdio.h>
#include<conio.h>
struct person
{
int height;
float weight;
};
int main()
{
struct person *Ptr, person1;
Ptr = &person1;
printf("Enter height:");
scanf("%d", &Ptr->height);
printf("Enter weight:");
scanf("%f", &Ptr->weight);
printf("Displaying:\n");
printf(" height : %d\n", Ptr-> height );
printf("weight: %f", Ptr->weight);
return 0;
}
C Program to access structure members
using pointer- Method-2
#include <stdio.h>
#include <conio.h>
struct person
{
int height ;
float weight;
}person1;
int main()
{
struct person *Ptr;
Ptr = &person1;
printf("Enter height :");
scanf("%d", &Ptr-> height );
printf("Enter weight:");
scanf("%f", &Ptr->weight);
printf("Displaying:\n");
printf(" height : %d\n", Ptr-> height );
printf("weight: %f",
Ptr->weight);
return 0;
}
C Program to access structure members
using pointer- Method-3
#include<stdio.h>
#include<conio.h>
struct Player
{
char *team_name;
int members;
char captain[30];
}team = {"Austrailia",11,"Mourtin"}
, *sptr = &team;
int main()
{
printf("\nTeam :
%s",(*sptr).team_name);
printf("\nMemebers : %d",sptr->members);
printf("\nCaptain :
%s",(*sptr).captain);
return 0;
}
C Program to access structure members
using pointer- Method-4
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct std
{
int id;
char name[50];
float percentage;
};
int main()
{
int i;
struct std record1 = {1, "Ram", 88.63};
struct std *ptr;
ptr = &record1;
printf("Records of STUDENT1:
\n");
printf(" Id is: %d \n", ptr->id);
printf(" Name is: %s \n", ptr->name);
printf(" Percentage is: %f \n\n",
ptr->percentage);
return 0;
}
C Program to accessing structure Using
Indirection Operator and Pointer-Method-1
#include <stdio.h>
#include<conio.h>
int main(int argc, char *argv[])
{
struct std
{
char name[10];
int roll;
int marks;
}stud1 = {"Madhur",70,80};
struct std *ptr;
ptr = &stud1;
printf("Roll Number :
%d",(*ptr).roll);
printf("Marks of Student :
%d",(*ptr).marks);
return 0;
}
C Program to accessing structure Using
Indirection Operator and Pointer-Method-2
#include <stdio.h>
#include<conio.h>
int main(int argc, char *argv[])
{
struct std
{
char name[10];
int roll;
int marks;
} stud1 = {"Madhur",70,80};
struct std *ptr;
ptr = &stud1;
printf("Roll Number :
%d",(ptr)->roll);
printf("Marks of Student :
%d",(ptr)->marks);
return 0;
}
C Program to accessing structure Using
Dynamic memory allocation
Method-1
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct person1
{
int age;
float height;
float weight;
char name[30];
};
int main()
{ struct person1 *ptr;
int i, n;
printf("Enter number of persons:
");
scanf("%d", &n);
ptr = (struct person1*)
malloc(n*sizeof(struct person1));
for(i = 0; i < n; ++i)
{
printf("Enter first name and age respectively: ");
scanf("%s%d",
&(ptr+i)->name, &(ptr+i)->age);
}
printf("Displaying
Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\t Age: %d\n",
(ptr+i)->name, (ptr+i)->age);
return 0;
}
C Program for Union
C Program to accessing member using
union-Method-1
#include <stdio.h>
#include<conio.h>
union Data1
{
int i;
float f;
char str[20];
};
int main( )
{
union Data1 data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C
Programming");
printf( "data.i :
%d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str :
%s\n", data.str);
return 0;
}
C Program to accessing member using
union-Method-2
#include <stdio.h>
#include <string.h>
union emp
{
int Age;
char Name[100];
char Dept[100];
float Salary;
};
int main()
{
union emp emp1;
union emp emp2;
emp1.Age = 40;
strcpy(emp1.Name, "Chris");
strcpy(emp1.Dept, "Science");
emp1.Salary = 62240.70;
printf("\nDetails of the First Employee \n");
printf(" Employee Age = %d
\n", emp1.Age);
printf(" Employee Name =
%s \n", emp1.Name);
printf(" Employee
Department = %s \n", emp1.Dept);
printf(" Employee Salary =
%.2f \n\n", emp1.Salary);
printf("Details of the Second Employee
\n" );
emp2.Age = 20;
printf(" Employee Age = %d
\n", emp2.Age );
strcpy(emp2.Name,
"David");
printf(" Employee Name =
%s \n", emp2.Name );
strcpy(emp2.Dept, "Technology" );
printf(" Employee
Department = %s \n ", emp2.Dept );
emp2.Salary = 25700.20;
printf(" Employee Salary =
%.2f \n ", emp2.Salary );
return 0;
}
C Program to accessing member using
union-Method-3
#include <stdio.h>
#include<conio.h>
#include <string.h>
union Emp
{
char Name[32];
int Age;
float Salary;
};
int main()
{
union Emp
employee;
strcpy(employee.Name,
"Rahul");
printf("Name
= %s Address =%p\n", employee.name, &employee.Name);
employee.Age
= 20;
printf("Age
= %d Address =%p\n", employee.age, &employee.age);
employee.Salary
= 4234.5;
printf("Salary
= %f Address = %p\n", employee.salary, &employee.Salary);
printf("\nName
= %s\n", employee.name);
printf("Age
= %d\n",employee.Age);
printf("Salary
= %f\n",employee.Salary);
return
0;
}
Introduction to C Language
Top Structure and Union C Program |
C Program for Structure and Union |
C Program for Structure |
C program to accessing members of the structure (Operations on individual
members)-Method-1 |
#include <stdio.h> #include <stdlib.h> #include<string.h> struct std {
int roll_no; char name[10]; }student={4,"Ram"}; int main() { printf("Roll number=%d\n", student .roll_no); printf("student name=%s", student .name); return 0; } |
C program to accessing members of the structure (Operations on
individual members)-Method-2 |
#include <stdio.h> #include <stdlib.h> #include<string.h> struct std {
int roll_no;
char name[10]; }; int main() { struct std student ={4,"Ram"}; printf("Roll number=%d\n", student .roll_no); printf("student name=%s", student .name); return 0; } |
C program to accessing members of the structure (Operations on
individual members)-Method-3 |
#include <stdio.h> #include <stdlib.h> #include<string.h> struct student {
int roll_no; char name[10]; }; int main() {
struct student std; printf("Enter a Roll Number"); scanf("%d",&std.roll_no); printf("Enter name"); scanf("%s",&std.name); printf("Roll number=%d\n", std.roll_no); printf("student name=%s", std.name); return 0; } |
C Program to print sum of
two number using structure ( Operations on Structures through individual members )-Method-1 |
#include <stdio.h> #include <stdlib.h> struct add {
int foot;
int inch; } sum={4,8}; int main() {
int sum1; sum1=sum.foot+sum.inch; printf("addition of two
number=%d", sum1); return 0; } |
C Program to print sum of
two number using structure ( Operations on Structures through individual members )-Method-2 |
#include <stdio.h> #include <stdlib.h> struct add {
int foot;
int inch; }; struct add sum={4,8}; int main() {
int sum1; sum1=sum.foot+sum.inch; printf("addition of two
number=%d", sum1); return 0; } |
C Program to print sum of two number using structure (Operations on Structures through individual members )-Method-3 |
#include <stdio.h> #include <stdlib.h> struct add {
int foot;
int inch; }; int main() {
int sum1; struct add sum={4,8}; sum1=sum.foot+sum.inch;
printf("addition of two number=%d", sum1); return 0; } |
C Program to print sum of
two number using structure ( Operations on Structures through individual members )-Method-4 |
#include <stdio.h> #include <stdlib.h> struct add {
int foot;
int inch; }; int main() {
int sum1;
struct add sum; printf("Enter feet: "); scanf("%d", &sum.foot); printf("Enter inch: "); scanf("%d", &sum.inch); sum1=sum.foot+sum.inch; printf("addition of two
number=%d", sum1); return 0; } |
C Program to implement of
array using structure-Method-1 |
#include<stdio.h> #include<conio.h> struct Point { int x, y; }; void main() { struct Point array[15]; array[0].x = 10; array[0].y = 20; printf("%d %d", array[0].x, array[0].y); getch(); } |
C Program to implement of
array using structure-Method-2 |
#include<stdio.h> #include<conio.h> #include <string.h> struct student { int roll_no; char name[10]; }; int main() { int i; struct student std[5]; printf("Enter Records of 5 students"); for(i=0;i<5;i++) { printf("\nEnter Rollno:"); scanf("%d",&std[i].roll_no); printf("\nEnter Name:"); scanf("%s",&std[i].name); } printf("\nStudent Information List:"); for(i=0;i<5;i++) { printf("\nRollno:%d, Name:%s",std[i].rollno,std[i].name); } return 0; } |
C Program to implement of
array using structure for book keeping record-Method-1 |
#include<stdio.h> #include<string.h> #include<conio.h> struct BookRecord {
char book_name[25];
int pages;
char author[25];
float price;
}b1[4] = {
{"Book1",700,"YPK“,250.00},
"Book2",500,"AAK",350.00},
{"Book3",120,"HST",450.00} {"Book3",120,"HST",450.00} }; void main() { printf("\nBook Name : %s",b1[0].bname); printf("\nBook Pages : %d",b1[0].pages); printf("\nBook Author : %s",b1[0].author); printf("\nBook Price : %f",b1[0].price); printf("\nBook Name : %s",b1[1].bname); printf("\nBook Pages : %d",b1[1].pages); printf("\nBook Author : %s",b1[1].author); printf("\nBook Price : %f",b1[1].price); printf("\nBook Name : %s",b1[2].bname); printf("\nBook Pages : %d",b1[2].pages); printf("\nBook Author : %s",b1[2].author); printf("\nBook Price : %f",b1[2].price); getch(); } |
C Program to implement of
array using structure for book keeping record-Method-2 |
#include<stdio.h> #include<string.h> #include<conio.h> struct BookRecord {
char[25] b_name;
int pages;
int price; } book[3]; int main(int argc, char *argv[]) { int i; 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);
} printf("\n---Book Details -----
");
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; } |
C Program to implement of
array using structure for student information record-Method-1 |
#include<stdio.h> #include<conio.h>
#include<stdlib.h> struct student { char name[25]; int std_id; float marks; }; void main() { struct student s1,s2,s3; printf("Enter the name, id, and marks
of student 1 "); scanf("%s %d
%f",s1.name,&s1.std_id, &s1.marks); printf("Enter the name, id, and marks
of student 2 "); scanf("%s %d
%f",s2.name,&s2.std_id,&s2.marks); printf("Enter
the name, id, and marks of student 3 ");
scanf("%s %d
%f",s3.name,&s3.std_id,&s3.marks);
printf("Printing the details....\n"); printf("%s %d
%f\n",s1.name,s1.std_id,s1.marks);
printf("%s %d
%f\n",s2.name,s2.std_id,s2.marks);
printf("%s %d
%f\n",s3.name,s3.std_id,s3.marks);
getch(); } |
C Program to implement of structure using function. |
#include <stdio.h> #include <stdlib.h> #include<string.h> struct student { char name[30]; int rollno; int marks; }; void display(char name[],
int rollno, int marks); int main() { struct student std =
{"Ram", 1, 88}; display(std.name, std.rollno,
std.marks); return 0; } void display(char name[],
int rollno, int marks) { printf("Name:
%s\n", name); printf("Roll no:
%d\n", rollno); printf("Marks:
%d\n", marks); printf("\n"); } |
C Program to access structure members
using pointer-Method-1 |
#include <stdio.h> #include<conio.h> struct person {
int height;
float weight; }; int main() {
struct person *Ptr, person1;
Ptr = &person1;
printf("Enter height:");
scanf("%d", &Ptr->height);
printf("Enter weight:");
scanf("%f", &Ptr->weight);
printf("Displaying:\n");
printf(" height : %d\n", Ptr-> height );
printf("weight: %f", Ptr->weight);
return 0; } |
C Program to access structure members
using pointer- Method-2 |
#include <stdio.h> #include <conio.h> struct person {
int height ;
float weight; }person1; int main() {
struct person *Ptr;
Ptr = &person1;
printf("Enter height :");
scanf("%d", &Ptr-> height );
printf("Enter weight:");
scanf("%f", &Ptr->weight);
printf("Displaying:\n");
printf(" height : %d\n", Ptr-> height ); printf("weight: %f",
Ptr->weight);
return 0; } |
C Program to access structure members
using pointer- Method-3 |
#include<stdio.h> #include<conio.h> struct Player {
char *team_name;
int members;
char captain[30]; }team = {"Austrailia",11,"Mourtin"}
, *sptr = &team; int main() { printf("\nTeam :
%s",(*sptr).team_name); printf("\nMemebers : %d",sptr->members); printf("\nCaptain :
%s",(*sptr).captain); return 0; } |
C Program to access structure members
using pointer- Method-4 |
#include <stdio.h> #include<conio.h> #include <string.h> struct std {
int id;
char name[50];
float percentage; }; int main() {
int i;
struct std record1 = {1, "Ram", 88.63};
struct std *ptr;
ptr = &record1; printf("Records of STUDENT1:
\n"); printf(" Id is: %d \n", ptr->id); printf(" Name is: %s \n", ptr->name); printf(" Percentage is: %f \n\n",
ptr->percentage);
return 0; }
|
C Program to accessing structure Using
Indirection Operator and Pointer-Method-1 |
#include <stdio.h> #include<conio.h> int main(int argc, char *argv[]) { struct std {
char name[10];
int roll;
int marks; }stud1 = {"Madhur",70,80}; struct std *ptr; ptr = &stud1; printf("Roll Number :
%d",(*ptr).roll); printf("Marks of Student :
%d",(*ptr).marks); return 0; } |
C Program to accessing structure Using
Indirection Operator and Pointer-Method-2 |
#include <stdio.h> #include<conio.h> int main(int argc, char *argv[]) { struct std {
char name[10];
int roll;
int marks; } stud1 = {"Madhur",70,80}; struct std *ptr; ptr = &stud1; printf("Roll Number :
%d",(ptr)->roll); printf("Marks of Student :
%d",(ptr)->marks); return 0; } |
C Program to accessing structure Using
Dynamic memory allocation Method-1 |
#include <stdio.h> #include<conio.h> #include <stdlib.h> struct person1 { int age; float height; float weight; char name[30]; }; int main() { struct person1 *ptr;
int i, n; printf("Enter number of persons:
"); scanf("%d", &n); ptr = (struct person1*)
malloc(n*sizeof(struct person1)); for(i = 0; i < n; ++i)
{
printf("Enter first name and age respectively: "); scanf("%s%d",
&(ptr+i)->name, &(ptr+i)->age);
} printf("Displaying
Information:\n"); for(i = 0; i < n; ++i) printf("Name: %s\t Age: %d\n",
(ptr+i)->name, (ptr+i)->age);
return 0; } |
C Program for Union |
C Program to accessing member using
union-Method-1 |
#include <stdio.h> #include<conio.h> union Data1 { int i; float f; char str[20]; }; int main( ) { union Data1 data; data.i = 10; data.f = 220.5; strcpy( data.str, "C
Programming"); printf( "data.i :
%d\n", data.i); printf( "data.f : %f\n", data.f); printf( "data.str :
%s\n", data.str); return 0; } |
C Program to accessing member using
union-Method-2 |
#include <stdio.h> #include <string.h> union emp {
int Age;
char Name[100];
char Dept[100];
float Salary; }; int main() {
union emp emp1; union emp emp2;
emp1.Age = 40; strcpy(emp1.Name, "Chris"); strcpy(emp1.Dept, "Science"); emp1.Salary = 62240.70;
printf("\nDetails of the First Employee \n"); printf(" Employee Age = %d
\n", emp1.Age);
printf(" Employee Name =
%s \n", emp1.Name);
printf(" Employee
Department = %s \n", emp1.Dept);
printf(" Employee Salary =
%.2f \n\n", emp1.Salary); printf("Details of the Second Employee
\n" );
emp2.Age = 20;
printf(" Employee Age = %d
\n", emp2.Age ); strcpy(emp2.Name,
"David");
printf(" Employee Name =
%s \n", emp2.Name );
strcpy(emp2.Dept, "Technology" );
printf(" Employee
Department = %s \n ", emp2.Dept );
emp2.Salary = 25700.20;
printf(" Employee Salary =
%.2f \n ", emp2.Salary );
return 0; } |
C Program to accessing member using
union-Method-3 |
#include <stdio.h> #include<conio.h> #include <string.h> union Emp { char Name[32]; int Age; float Salary; }; int main() { union Emp
employee; strcpy(employee.Name, "Rahul"); printf("Name = %s Address =%p\n", employee.name, &employee.Name); employee.Age
= 20; printf("Age = %d Address =%p\n", employee.age, &employee.age); employee.Salary
= 4234.5; printf("Salary = %f Address = %p\n", employee.salary, &employee.Salary); printf("\nName = %s\n", employee.name); printf("Age = %d\n",employee.Age); printf("Salary = %f\n",employee.Salary); return
0; } |
Comments
Post a Comment