0% found this document useful (0 votes)
2 views24 pages

Lec 12 Structure and Union

The document provides an overview of structures and unions in C programming, detailing how to define and use them to create new data types that can hold multiple values. It includes example programs demonstrating the use of structures to store student information and the implementation of unions for efficient memory usage. Additionally, it discusses problems and examples related to complex numbers and the practical applications of structures and unions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Lec 12 Structure and Union

The document provides an overview of structures and unions in C programming, detailing how to define and use them to create new data types that can hold multiple values. It includes example programs demonstrating the use of structures to store student information and the implementation of unions for efficient memory usage. Additionally, it discusses problems and examples related to complex numbers and the practical applications of structures and unions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSE 101

Computer Programming

structure & union


Structure
Structure in C
•Using structure you can define a new data type. So you will
have more than the 4 data types.

•So by structure we can create a new data type. In the data


type we can keep more than one integers or characters or
others.

•Using structure is very important if you want to keep some


part of the program private. In C++ it is all about structures
Structure of Structure

•Like function we have to define the structure. The syntax is


like: struct name
{
datatype name;
datatype name;
………..
………..
}
Write a c-program to show your name and roll number with
structure?
struct student one;
#include <stdio.h> one.id=10;
one.name="Romjan Ali";
int main()
printf("ID: %d\n",one.id);
{
printf("%s\n",one.name);
struct student {
return 0;
int id;
char* name;
} ID: 10
Romjan Ali
};
Process returned 0 (0x0) execution time : 1.671 s
Press any key to continue.
Write a c-program to show your name and roll number with
structure?
Value can be taken from user

struct student one;


#include <stdio.h>
scanf("%d",&one.id);
#include <string.h>
scanf("%s",one.name);
printf("ID: %d\n",one.id);
int main() printf("name:%s\n",one.name);
{ return 0;
struct student {
int id; }
char name[40];
}; 01
romjan
ID: 1
name:romjan

Process returned 0 (0x0) execution time : 34.139 s


Press any key to continue.
Write a c-program to show your roll,name and mark number
with structure?
#include <stdio.h>
#include <string.h>

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++){

#include <stdio.h> printf("ID: %d \t ",s[i].id);


#include <string.h> printf("name:%s\t\t",s[i].name);
printf("Marks:%d\n",s[i].mark);
int main() }
{ printf("arrange it in ascending order\n");
struct student { for(i=0;i<5;i++){
int id;
char name[40]; for(j=0;j<4;j++){
int mark;
}; if(s[j].mark > s[j+1].mark) {

struct student s[5],sc; sc=s[j];


int i,j; s[j]=s[j+1];
for(i=0;i<5;i++){ s[j+1]=sc;
printf("ID of the %dth student\t",i+1); }
scanf("%d",&s[i].id); }
printf("Nick name of the %dth student\t",i+1); }
scanf("%s",s[i].name); for(i=0;i<5;i++){
printf("marks of the %d th student\t\n",i+1);
scanf("%d",&s[i].mark);
printf("ID %d \t %s \t Marks:%d\n",s[i].id,s[i].name, s[i].mark);
}
}
return 0;

}
Output

ID of the 1th student 1


Nick name of the 1th student a
marks of the 1 th student
ID: 1 name:a Marks:12
12
ID: 2 name:b Marks:2
ID of the 2th student 2
ID: 3 name:c Marks:33
Nick name of the 2th student b
ID: 4 name:d Marks:55
marks of the 2 th student
ID: 5 name:t Marks:1
2
arrange it in ascending order
ID of the 3th student 3
ID 5 t Marks:1
Nick name of the 3th student c
ID 2 b Marks:2
marks of the 3 th student
ID 1 a Marks:12
33
ID 3 c Marks:33
ID of the 4th student 4
ID 4 d Marks:55
Nick name of the 4th student d
marks of the 4 th student
Process returned 0 (0x0) execution time : 45.081 s
55
Press any key to continue.
ID of the 5th student 5
Nick name of the 5th student t
marks of the 5 th student
1
Example
Create a structure that contains the student name, roll no and
exam mark for CSE 101 and define 3 student variable.
Example:
• Create a structure as the previous example and input the values of
the variables inside the structures.
Example
• Put values like the example before in an array of structure. Finally
show the roll and marks in ascending order of roll.
Problems.

1. Suppose you have an array of 10 structures. Now in every structure


there are two integers. One will be the input and other you will
have to find out. If the input integer is prime then it will be 1
otherwise 0.

2. Suppose there are two elements in a structure. First is a sentence


and second is an integer. The sentence will be input and you will
have to find the value of the integer which will be the number of
letters of the sentence.
Solve 1
Solve 2
Complex Number Using Structure

• A good way to understand the use of structure is complex number.


Which has two parts. Real and imaginary.

• 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

• Now we will do the same thing with two difference.


• We will multiply two numbers not add.
• We will not use a function for multiplication.

• (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>

struct s{ structure variable took 24


char ch; Union variable took 16
int n; Process returned 0 (0x0) execution time : 1.187 s
char str[16]; Press any key to continue.

}see;

union u {

char ch; See and uee may be


int n; changed without altering
char str[16]; first letter .
}uee;
int main()
{
int struct_size,union_size;
struct_size=sizeof(see);
union_size=sizeof(uee);

printf("structure variable took %d",struct_size);


printf("Union variable took %d",union_size);
return 0;
}
Justify the statement .’’ A union is a special data type available in C that allows to store different data types in the same
memory location.’’

#include <stdio.h> after fixing x value the coordinates of t will be 3 3


struct test1 {
int x, y; After fixing y value the coordinates of t will be 4 4
};
The coordinates of t1 are 1 2
union test { Process returned 0 (0x0) execution time : 1.841 s
int x, y; Press any key to continue.
};

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy