SlideShare a Scribd company logo
3
Most read
8
Most read
16
Most read
STRUCTURES AND UNIONS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
STRUCTURES
Arrays are used to store large set of data and manipulate them
but the disadvantage is that all the elements stored in an array
are to be of the same data type. If we need to use a collection of
different data type items it is not possible using an array.
When we require using a collection of different data items of
different data types we can use a structure.
Structure is a method of packing data of different types.
A structure is a convenient method of handling a group of
related data items of different data types.
Structure definition:
General format:
struct tag_name
{
datatype member1;
datatype member2;
…
…
};
A structure is usually defines before
main along with macro definitions.
In such cases the structure assumes
global status and all the functions
can access the structure.
Example:
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares a structure
to holds the details of four fields namely
title, author pages and price. These are
members of the structures.
Each member may belong to different or
same data type. The tag name can be
used to define objects that have the tag
names structure.
The structure we just declared is not a
variable by itself but a template for the
structure.
We can declare structure variables using the tag
name any where in the program.
For example the statement,
For example: struct lib_books book1,book2,book3;
declares book1,book2,book3 as variables of type
struct lib_books each declaration has four elements
of the structure lib_books.
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
struct lib_books, book1, book2, book3;
Structures do not occupy any memory until it is
associated with the structure variable such as book1.
The template is terminated with a semicolon.
While the entire declaration is considered as a
statement, each member is declared independently for
its name and type in a separate statement inside the
template.
The tag name such as lib_books can be used to declare
structure variables of its data type later in the program.
We can also combine both
template declaration and
variables declaration in one
statement.
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
Giving values to members:
The members themselves are not variables they should be linked to
structure variables in order to make them meaningful members.
The link between a member and a variable is established using the
member operator ‘.’ known as dot operator or period operator.
For example: Book1.price
scanf("%s",Book1.file);
scanf("%d",& Book1.pages);
strcpy(book1.title,"basic");
strcpy(book1.author,"Balagurusamy");
book1.pages=250;
book1.price=28.50;
Initializing structure:
Like other data type we can initialize structure when we
declare them.
As for initialization goes structure obeys the same set of rules
as arrays.
We initialize the fields of a structure by the following
structure declaration with a list containing values for each
fields as with arrays these values must be evaluate at compile
time.
struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}newstudent;
struct student newstudent
{
12345,
“kapildev”
“pes college”;
“cse”;
19;
};
This initializes the id_no field to 12345, the name field to "kapildev", the
address field to "pes college" the field combination to "cse" and the age
field to 19.
UNION
A union is a special data type available in C that allows storing
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 purposes.
Defining a Union:
To define a union, you must use the union statement in the same
way as you did while defining a structure. The union statement
defines a new data type with more than one member for your
program.
union [union name]
{
member definition;
member definition;
...
member definition;
};
union union_example
{
int integer;
float decimal;
char name[20];
};
The format of the union statement is as follows:
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf

More Related Content

PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Functions in c
PDF
Unit II chapter 4 Loops in C
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Constants Variables Datatypes by Mrs. Sowmya Jyothi
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Functions in c
Unit II chapter 4 Loops in C
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf

What's hot (20)

PPTX
C Programming Unit-5
PPTX
Functions in c language
PPTX
C Structures and Unions
PPTX
Overview of C Mrs Sowmya Jyothi
PPTX
PPTX
PPTX
Handling of character strings C programming
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
C programming - String
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
C Programming: Structure and Union
PPT
16717 functions in C++
 
PPTX
Typedef
PPT
Storage classes
PPTX
Structures and Unions
PPT
Strings Functions in C Programming
PPTX
Managing input and output operations in c
DOC
String in c
PDF
Managing I/O in c++
PPTX
C programming Training in Ambala ! Batra Computer Centre
C Programming Unit-5
Functions in c language
C Structures and Unions
Overview of C Mrs Sowmya Jyothi
Handling of character strings C programming
Unit ii chapter 1 operator and expressions in c
C programming - String
Unit ii chapter 2 Decision making and Branching in C
C Programming: Structure and Union
16717 functions in C++
 
Typedef
Storage classes
Structures and Unions
Strings Functions in C Programming
Managing input and output operations in c
String in c
Managing I/O in c++
C programming Training in Ambala ! Batra Computer Centre
Ad

Similar to STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf (20)

PDF
Lk module4 structures
PPTX
Structure prespentation
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
PPTX
Programming in C
PPTX
CPU : Structures And Unions
PPTX
C programing -Structure
PPT
2 lesson 2 object oriented programming in c++
PPTX
Structures in c language
PPTX
Structures in c language
PDF
structure and union1.pdf
PDF
Chapter 13.1.9
PDF
Structures in c++
PDF
Structures in c++
PDF
Unit 4 qba
PPTX
Chapter4.pptx
PPT
Lecture 19 - Struct and Union
PPT
Structures and Unions in C-Language with Examples.ppt
PPT
structures_v1.ppt
PPT
structures_v1.ppt
PPT
Introduction-to-structures-using-C-programming.ppt
Lk module4 structures
Structure prespentation
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Programming in C
CPU : Structures And Unions
C programing -Structure
2 lesson 2 object oriented programming in c++
Structures in c language
Structures in c language
structure and union1.pdf
Chapter 13.1.9
Structures in c++
Structures in c++
Unit 4 qba
Chapter4.pptx
Lecture 19 - Struct and Union
Structures and Unions in C-Language with Examples.ppt
structures_v1.ppt
structures_v1.ppt
Introduction-to-structures-using-C-programming.ppt
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Pre independence Education in Inndia.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
Pre independence Education in Inndia.pdf
Insiders guide to clinical Medicine.pdf
Supply Chain Operations Speaking Notes -ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems

STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf

  • 1. STRUCTURES AND UNIONS REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
  • 2. STRUCTURES Arrays are used to store large set of data and manipulate them but the disadvantage is that all the elements stored in an array are to be of the same data type. If we need to use a collection of different data type items it is not possible using an array. When we require using a collection of different data items of different data types we can use a structure. Structure is a method of packing data of different types. A structure is a convenient method of handling a group of related data items of different data types.
  • 3. Structure definition: General format: struct tag_name { datatype member1; datatype member2; … … }; A structure is usually defines before main along with macro definitions. In such cases the structure assumes global status and all the functions can access the structure.
  • 4. Example: struct lib_books { char title[20]; char author[15]; int pages; float price; }; The keyword struct declares a structure to holds the details of four fields namely title, author pages and price. These are members of the structures. Each member may belong to different or same data type. The tag name can be used to define objects that have the tag names structure. The structure we just declared is not a variable by itself but a template for the structure.
  • 5. We can declare structure variables using the tag name any where in the program. For example the statement, For example: struct lib_books book1,book2,book3; declares book1,book2,book3 as variables of type struct lib_books each declaration has four elements of the structure lib_books.
  • 6. struct lib_books { char title[20]; char author[15]; int pages; float price; }; struct lib_books, book1, book2, book3; Structures do not occupy any memory until it is associated with the structure variable such as book1. The template is terminated with a semicolon. While the entire declaration is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template. The tag name such as lib_books can be used to declare structure variables of its data type later in the program.
  • 7. We can also combine both template declaration and variables declaration in one statement. struct lib_books { char title[20]; char author[15]; int pages; float price; } book1,book2,book3;
  • 8. Giving values to members: The members themselves are not variables they should be linked to structure variables in order to make them meaningful members. The link between a member and a variable is established using the member operator ‘.’ known as dot operator or period operator. For example: Book1.price scanf("%s",Book1.file); scanf("%d",& Book1.pages); strcpy(book1.title,"basic"); strcpy(book1.author,"Balagurusamy"); book1.pages=250; book1.price=28.50;
  • 9. Initializing structure: Like other data type we can initialize structure when we declare them. As for initialization goes structure obeys the same set of rules as arrays. We initialize the fields of a structure by the following structure declaration with a list containing values for each fields as with arrays these values must be evaluate at compile time.
  • 10. struct student { int id_no; char name[20]; char address[20]; char combination[3]; int age; }newstudent; struct student newstudent { 12345, “kapildev” “pes college”; “cse”; 19; }; This initializes the id_no field to 12345, the name field to "kapildev", the address field to "pes college" the field combination to "cse" and the age field to 19.
  • 11. UNION A union is a special data type available in C that allows storing 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 purposes. Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program.
  • 12. union [union name] { member definition; member definition; ... member definition; }; union union_example { int integer; float decimal; char name[20]; }; The format of the union statement is as follows: