SlideShare une entreprise Scribd logo
1  sur  11
Exercise 1 on Structures
struct complex
{ int real;
int imaginary; };
main()
{
struct complex * p; // Declare pointer for object
// Memory allocation for object
p=(struct complex *) malloc(sizeof(struct complex));
printf( " Please give the values for complex number : " );
scanf( "%d%d", &(p->real), & (p->imaginary) );
printf( "Complex number : %d + %d i", p->real, p->imaginary );
}
Senem Kumova Metin
// A function to add two integers
int add(int a, int b)
{ int result= a+b;
return result;
}
struct complex { int r; int i; };
// A function to add two complex numbers
struct complex add ( struct complex a, struct complex b )
{ struct complex result;
result.r=a.r+b.r;
result.i=a.i+b.i;
return result; }
Exercise 2 : Define a function to add two
complex numbers
Exercise 3 on Structures
struct rectangular_prism
{ int height;
int width;
int length; };
int volume(struct rectangular_prism x)
{return x.height*x.width*x.length; }
int area (struct rectangular_prism x)
{
return 2*x.width*x.lenght +
2*x.lenght*x.height +
2*x.width*x.lenght;
}
Unions are collection of different datatype grouped
together under a single variable name for convienient handling.
syntax : union <union name >
{
datatype member1;
datatype member2;
};
union book
{
int pages;
char bookname[10];
char author[20];
float price;
};
Structure Arrays
It is an single entity representing a
Collection of data types of different
data types
It is an single entity representing a
Collection of data types of same data
types
Individual entries in a structure are
called Members
Individual entries in a array are called
array elements
The members of a structure are not
stored in sequence of memory
locations
The members of a array are stored in
sequence of memory locations
All the members of a structure can be
initialized
Only the first member of an union can be
initialized
Individual structure elements are
referred through the use of .(dot or
membership) operator
Individual array elements are accessed by
its name followed by the square braces[]
within which the index is placed
Initialization of elements can be done
only during structure definition
Initialization of elements can be done
during array declaration
Structure definition reserve enough
memory space for its members
Array declaration reserve enough memory
space for its elements
The keyword ‘struct’ tells us what we
are dealing with structure
The is no keyword to represent arrays
Structure Union
Keyword : struct Keyword : union
Each member in a structure occupies
and uses its own memory space
All the members of a union use the same
memory space
More memory space is required since
each member is stored in a separate
memory locations
Less memory space is required since all
member is stored in the same memory
locations
All the members of a structure can be
initialized
Only the first member of an union can be
initialized
The variables that make up the
structure are called Structure
variable
The variables that make up the union are
called union variable
Individual structure elements are
referred through the use of .(dot or
membership) operator
Individual union elements are referred
through the use of .(dot or membership )
operator
Structure is a user-defined data type.
Union is a user-defined data type.
Possible to have one structure within
another structure
Unions within union is possible
Session Summary
 The union is another compound data type may hold the variables of different types and
sizes with the compiler keeping track of the size.
The memory space reserved for a union members is large enough to store its largest
member
 The keyword “union” begins the union declaration followed by the tag (i.e., union
name)
 Within the braces union members are declared
EXERCISES
1. Define a union called student that will describe the following information student
name,class,rollno, subject marks & total. Using student declare an array stu_list
with 30 elements. Write program in C to read the information about all the 30
students and to display the information?

Contenu connexe

Similaire à UNIONS.pptx

Similaire à UNIONS.pptx (20)

Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Structure c
Structure cStructure c
Structure c
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
C programming session 09
C programming session 09C programming session 09
C programming session 09
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
User Defined Datatypes in C++ (Union, enum, class)
User Defined Datatypes in C++  (Union, enum, class)User Defined Datatypes in C++  (Union, enum, class)
User Defined Datatypes in C++ (Union, enum, class)
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Chapter 7 (Part I) - User Defined Datatypes.pdf
Chapter 7 (Part I) - User Defined Datatypes.pdfChapter 7 (Part I) - User Defined Datatypes.pdf
Chapter 7 (Part I) - User Defined Datatypes.pdf
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structures
StructuresStructures
Structures
 
Structures
StructuresStructures
Structures
 
Structures
StructuresStructures
Structures
 
Str
StrStr
Str
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 

Dernier

School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 

Dernier (20)

School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

UNIONS.pptx

  • 1.
  • 2. Exercise 1 on Structures struct complex { int real; int imaginary; }; main() { struct complex * p; // Declare pointer for object // Memory allocation for object p=(struct complex *) malloc(sizeof(struct complex)); printf( " Please give the values for complex number : " ); scanf( "%d%d", &(p->real), & (p->imaginary) ); printf( "Complex number : %d + %d i", p->real, p->imaginary ); }
  • 3. Senem Kumova Metin // A function to add two integers int add(int a, int b) { int result= a+b; return result; } struct complex { int r; int i; }; // A function to add two complex numbers struct complex add ( struct complex a, struct complex b ) { struct complex result; result.r=a.r+b.r; result.i=a.i+b.i; return result; } Exercise 2 : Define a function to add two complex numbers
  • 4. Exercise 3 on Structures struct rectangular_prism { int height; int width; int length; }; int volume(struct rectangular_prism x) {return x.height*x.width*x.length; } int area (struct rectangular_prism x) { return 2*x.width*x.lenght + 2*x.lenght*x.height + 2*x.width*x.lenght; }
  • 5. Unions are collection of different datatype grouped together under a single variable name for convienient handling. syntax : union <union name > { datatype member1; datatype member2; }; union book { int pages; char bookname[10]; char author[20]; float price; };
  • 6.
  • 7. Structure Arrays It is an single entity representing a Collection of data types of different data types It is an single entity representing a Collection of data types of same data types Individual entries in a structure are called Members Individual entries in a array are called array elements The members of a structure are not stored in sequence of memory locations The members of a array are stored in sequence of memory locations All the members of a structure can be initialized Only the first member of an union can be initialized Individual structure elements are referred through the use of .(dot or membership) operator Individual array elements are accessed by its name followed by the square braces[] within which the index is placed Initialization of elements can be done only during structure definition Initialization of elements can be done during array declaration Structure definition reserve enough memory space for its members Array declaration reserve enough memory space for its elements The keyword ‘struct’ tells us what we are dealing with structure The is no keyword to represent arrays
  • 8.
  • 9. Structure Union Keyword : struct Keyword : union Each member in a structure occupies and uses its own memory space All the members of a union use the same memory space More memory space is required since each member is stored in a separate memory locations Less memory space is required since all member is stored in the same memory locations All the members of a structure can be initialized Only the first member of an union can be initialized The variables that make up the structure are called Structure variable The variables that make up the union are called union variable Individual structure elements are referred through the use of .(dot or membership) operator Individual union elements are referred through the use of .(dot or membership ) operator Structure is a user-defined data type. Union is a user-defined data type. Possible to have one structure within another structure Unions within union is possible
  • 10. Session Summary  The union is another compound data type may hold the variables of different types and sizes with the compiler keeping track of the size. The memory space reserved for a union members is large enough to store its largest member  The keyword “union” begins the union declaration followed by the tag (i.e., union name)  Within the braces union members are declared
  • 11. EXERCISES 1. Define a union called student that will describe the following information student name,class,rollno, subject marks & total. Using student declare an array stu_list with 30 elements. Write program in C to read the information about all the 30 students and to display the information?