Lec 12 Structure and Union
Lec 12 Structure and Union
Computer Programming
int main()
{
struct student { 01
int id; atiq
char name[40]; 98
int mark; ID: 1 name:atiq Marks:98
};
Process returned 0 (0x0) execution time : 12.096 s
struct student one; Press any key to continue.
scanf("%d",&one.id);
scanf("%s",one.name);
scanf("%d",&one.mark);
printf("ID: %d ",one.id);
printf("name:%s\t",one.name);
printf("Marks:%d\n",one.mark);
return 0;
}
Write a c-program to show many student’s roll,name and mark number with structure?
#include <stdio.h>
printf("Nick name of the
%dth student\t",i+1);
int main()
scanf("%s",s[i].name);
{
printf("marks of the %dth
student\t\n",i+1);
struct student {
scanf("%d",&s[i].mark);
int id;
char name[40];
int mark;
}
};
for(i=0;i<n;i++){
int n;
printf("Enter the value of n\n");
printf("ID: %d ",s[i].id);
scanf("%d",&n);
struct student s[n];
printf("name:%s\t",s[i].name);
int i;
printf("Marks:%d\n",s[i].mark);
for(i=0;i<n;i++){
}
printf("ID of the %dth
student\t\n",i+1);
return 0;
scanf("%d",&s[i].id);
}
Arrange it in ascending Order :
for(i=0;i<5;i++){
}
Output
• So two numbers will be within the structure and we will use functions
to add, subtract or multiply.
• In the next program you will have to give output two complex
numbers and you want and you will get the result.
Complex Number Using Structure
Complex Number Using Structure
• (a.re+j*a.img) (b.re+j*b.img)=(a.re*b.re-a.img*b.img)+
j*(a.re*b.img+a.img*b.re)
Complex Number Using Structure
Union
A union is a special data type available in C that allows to store different data types in the same memory
location. You can define a union with many members, but only one member can contain a value at any given
time. Unions provide an efficient way of using the same memory location for multiple-purpose.
Structure of Union
union union_name {
datatype field_name;
datatype field_name;
// more variables
};
#include <stdio.h>
}see;
union u {
int main() {
struct test1 t1={1,2};
union test t;
t.x = 3; // t.y also gets value 3
printf ("after fixing x value the coordinates of t will be %d %d\n\n",t.x, t.y);
t.y = 4; // t.x is also updated to 4
printf ("After fixing y value the coordinates of t will be %d %d\n\n", t.x, t.y);
printf("The coordinates of t1 are %d %d",t1.x,t1.y);
return 0;
}
#include<stdio.h> Unions inside structure
struct student {
union { //anonymous union (unnamed union)
char name[10];
int roll;
};
int mark;
};
int main() {
struct student stud;
char choice; You can enter your name or roll number
printf("\n You can enter your name or roll number "); Do you want to enter the name (y or n): n
printf("\n Do you want to enter the name (y or n): ");
scanf("%c",&choice); Enter roll number1137
if(choice=='y'||choice=='Y') {
printf("\n Enter name: "); Roll:1137
scanf("%s",stud.name); Enter marks19
printf("\n Name:%s",stud.name);
} Marks:19
else { Process returned 0 (0x0) execution time : 19.469 s
printf("\n Enter roll number"); Press any key to continue.
scanf("%d",&stud.roll);
printf("\n Roll:%d",stud.roll);
}
printf("\n Enter marks");
scanf("%d",&stud.mark);
printf("\n Marks:%d",stud.mark);
return 0;
}