SlideShare a Scribd company logo
2
Most read
9
Most read
20
Most read
•STRUCTURE
•UNIONS
1. INTRODUCTION
 C’ Supports a constructed data type known
as STRUCTURE, Which is a method of
packing data of different types.
WHAT IS STRUCTURE ?
 A Structure is a convenient tool for handling
a group of logically related data items.
 i.e. It can be used to represent a set of
attributes, such as : student_name , roll_no
A structure defination creates a
formet that may be used to declare
structure variables.
i.e. Consider a book database
consisting of a book name, Author,
Number of pages and price.
We can define a structure to hold this
information as follows:
Struct tag_name
{
data_type member1;
data_type member2;
… …
……… …..
};
1. The template terminated with a semicolon
2. While the entire declarationn considered as a
satement, each member is declared
indepandently for its name and type in a
separate statement inside the teplate.
3. The tag name such as book_bank can be
used to declare structure variables of its
type,letter in the program.
Struct book_bank
{
Char title[20];
Char author[15];
Int pages;
Float price;
};
 The keyword struct declares a structure to
hold the details of four fields, namely
title,author,pages and price.
 These fields are called structure elements or
members and each member belong to
different type of data.
 Book_bank is the name of the structure and
also called ‘STRUCTURE TAG’.
title
author
pages
price
Array of 15
characters
integer
float
Array of 20 characters
 The link between a member and variable is
established using the member operator ‘.’
which is also known as ‘dot operator’ or
‘period operator’.
 i.e.
book1.price
 Is the variable represnting the price of book1
and can be treated like any other ordinary
variable.
 Here is how we would assign values to the
members of book1:
strcpy(book1.title, “COMPUTER”);
strcpy(book1.author, “XYZ”);
book1.pages=250;
book1.price=29.99;
 We can also use scanf to give the values
through keyboard.
 A Structure must be declared as static if it is
to be initialized inside a function.
 main()
{
static struct
{
int weight;
float height;
}
student = (60,180.75);
…..
}
 If there are fewer initialization than that of
member variables in the structure. The
remaining member variables are initialized to
zero.
 i.e. if we don’t know the number of pages in
book:
struct book b1={“let us
C”,”kanetkar”,0,150.50};
 We use structure todescribe the format of a
number related variables.
 In such cases, we may declare array of
structures,each element of an array represent
a structure variable
 i.e. struct class student[100]
 This defines an array called student , that
consists of 100 element.
 Each element is defined to be of the type
struct class.
 An array of structure is stored inside the
memory in the same way as a multi-
dimensional array.
 C permits the use of array as a structure
members.
 We can use single or multi-dimensional array
of type int or float.
 i.e. Struct marks
{
int number;
float subject[3];
} student[2];
 The main philosophy of c language is the use
of functions. C supports the passing of
structure values as arguments to function.
 There are 3 methods by which the values of a
structure can be transfferd from one function
to another:
1. To pass each member of the structure as an
argument of function call.
2. Passing of a copy of the entire structure to
the called function
3. Pointers to pass the structure as an
argument.
 General formet of sending a copy of a
structure to thr called function:
data_type function name(st_name)
srtuct_type st_name;
{
…
return(expression);
}
 Unions are a concept borrowed from
structures and therefore follow the same
syntax as structures.
 Major diffferance in terms of Storage:
In structures each member has its
own storage location
In unions all members use the same
location
 Union may contain many members of
different type but can handle only one
member at a time.
 It can be declared using keyword union as
follows:
 union item
{
int m;
float x;
char c;
} code;
 Union union_name
{
data_type member1;
data_type mrmber2;
… … .. …..
} var1, var2, .. ;
Union book;
{
char title[15];
char *author;
int pages;
float price;
} b1, b2, b3;
 A union variable can be assigned to another
union variable.
 Address of the union variable is obtained
using the address of ‘&’ operator.
 It is to pass a union to function and a
function can return a union.
 We can use pointer to unions and within
unions.
 All members share the same storage area in
computers memory.

More Related Content

PPTX
Machine Learning Using Python
SavitaHanchinal
 
PPTX
STACKS IN DATASTRUCTURE
Archie Jamwal
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Unit 9. Structure and Unions
Ashim Lamichhane
 
PPTX
Machine learning ppt
Rajat Sharma
 
PPT
Unit 2 design of shaft
Somnath Kolgiri
 
PPTX
LEARNING WITHOUT BURDEN
Babita Khandwal
 
PPTX
Presentation on c structures
topu93
 
Machine Learning Using Python
SavitaHanchinal
 
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Pointers C programming
Appili Vamsi Krishna
 
Unit 9. Structure and Unions
Ashim Lamichhane
 
Machine learning ppt
Rajat Sharma
 
Unit 2 design of shaft
Somnath Kolgiri
 
LEARNING WITHOUT BURDEN
Babita Khandwal
 
Presentation on c structures
topu93
 

What's hot (20)

PPTX
Union in C programming
Kamal Acharya
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Structures in c language
tanmaymodi4
 
PPT
Union In language C
Ravi Singh
 
PPTX
File in C language
Manash Kumar Mondal
 
PPTX
C programing -Structure
shaibal sharif
 
PPTX
Union in c language
tanmaymodi4
 
PPT
C++ Arrays
أحمد محمد
 
PPTX
Structure in C language
CGC Technical campus,Mohali
 
PPTX
Pointers in c language
Tanmay Modi
 
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
PDF
Character Array and String
Tasnima Hamid
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
C Programming: Structure and Union
Selvaraj Seerangan
 
PPT
Strings
Mitali Chugh
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PPTX
Enums in c
Vijayananda Ratnam Ch
 
PPTX
C Programming Unit-5
Vikram Nandini
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Union in C programming
Kamal Acharya
 
Functions in c language
tanmaymodi4
 
Structures in c language
tanmaymodi4
 
Union In language C
Ravi Singh
 
File in C language
Manash Kumar Mondal
 
C programing -Structure
shaibal sharif
 
Union in c language
tanmaymodi4
 
C++ Arrays
أحمد محمد
 
Structure in C language
CGC Technical campus,Mohali
 
Pointers in c language
Tanmay Modi
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
Character Array and String
Tasnima Hamid
 
C Programming: Control Structure
Sokngim Sa
 
C Programming: Structure and Union
Selvaraj Seerangan
 
Strings
Mitali Chugh
 
Arrays in c
CHANDAN KUMAR
 
C Programming Unit-5
Vikram Nandini
 
Storage class in C Language
Nitesh Kumar Pandey
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Ad

Similar to C Structures and Unions (20)

PDF
structure and union1.pdf
HMCollegeInfo
 
PPTX
Unit-V.pptx
Mehul Desai
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PPTX
Structures in c language
Tanmay Modi
 
PDF
Unit 4 qba
Sowri Rajan
 
PDF
Structures in c++
Swarup Boro
 
PDF
Structures in c++
Swarup Kumar Boro
 
PDF
Lk module4 structures
Krishna Nanda
 
PPTX
Programming in C
MalathiNagarajan20
 
PDF
Chapter 13.1.9
patcha535
 
PDF
C structure and union
Thesis Scientist Private Limited
 
PPT
Lecture number three Structures (1).ppt
shahbazqadeer1
 
PPTX
Structure & union
lalithambiga kamaraj
 
DOC
9.structure & union
Shankar Gangaju
 
PDF
fundamental programming languagech#2 (3).pdf
gemchisgetahun
 
DOCX
Structure and Typedef
Acad
 
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
DOCX
Str
Acad
 
PPTX
Definition, Declaration of Structures in C.pptx
AnithaTAssistantProf
 
PPTX
Concept of c data types
Manisha Keim
 
structure and union1.pdf
HMCollegeInfo
 
Unit-V.pptx
Mehul Desai
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Structures in c language
Tanmay Modi
 
Unit 4 qba
Sowri Rajan
 
Structures in c++
Swarup Boro
 
Structures in c++
Swarup Kumar Boro
 
Lk module4 structures
Krishna Nanda
 
Programming in C
MalathiNagarajan20
 
Chapter 13.1.9
patcha535
 
C structure and union
Thesis Scientist Private Limited
 
Lecture number three Structures (1).ppt
shahbazqadeer1
 
Structure & union
lalithambiga kamaraj
 
9.structure & union
Shankar Gangaju
 
fundamental programming languagech#2 (3).pdf
gemchisgetahun
 
Structure and Typedef
Acad
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
Str
Acad
 
Definition, Declaration of Structures in C.pptx
AnithaTAssistantProf
 
Concept of c data types
Manisha Keim
 
Ad

Recently uploaded (20)

PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Zero Carbon Building Performance standard
BassemOsman1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 

C Structures and Unions

  • 2. 1. INTRODUCTION  C’ Supports a constructed data type known as STRUCTURE, Which is a method of packing data of different types. WHAT IS STRUCTURE ?  A Structure is a convenient tool for handling a group of logically related data items.  i.e. It can be used to represent a set of attributes, such as : student_name , roll_no
  • 3. A structure defination creates a formet that may be used to declare structure variables. i.e. Consider a book database consisting of a book name, Author, Number of pages and price. We can define a structure to hold this information as follows:
  • 4. Struct tag_name { data_type member1; data_type member2; … … ……… ….. };
  • 5. 1. The template terminated with a semicolon 2. While the entire declarationn considered as a satement, each member is declared indepandently for its name and type in a separate statement inside the teplate. 3. The tag name such as book_bank can be used to declare structure variables of its type,letter in the program.
  • 6. Struct book_bank { Char title[20]; Char author[15]; Int pages; Float price; };
  • 7.  The keyword struct declares a structure to hold the details of four fields, namely title,author,pages and price.  These fields are called structure elements or members and each member belong to different type of data.  Book_bank is the name of the structure and also called ‘STRUCTURE TAG’.
  • 9.  The link between a member and variable is established using the member operator ‘.’ which is also known as ‘dot operator’ or ‘period operator’.  i.e. book1.price  Is the variable represnting the price of book1 and can be treated like any other ordinary variable.
  • 10.  Here is how we would assign values to the members of book1: strcpy(book1.title, “COMPUTER”); strcpy(book1.author, “XYZ”); book1.pages=250; book1.price=29.99;  We can also use scanf to give the values through keyboard.
  • 11.  A Structure must be declared as static if it is to be initialized inside a function.  main() { static struct { int weight; float height; } student = (60,180.75); ….. }
  • 12.  If there are fewer initialization than that of member variables in the structure. The remaining member variables are initialized to zero.  i.e. if we don’t know the number of pages in book: struct book b1={“let us C”,”kanetkar”,0,150.50};
  • 13.  We use structure todescribe the format of a number related variables.  In such cases, we may declare array of structures,each element of an array represent a structure variable  i.e. struct class student[100]  This defines an array called student , that consists of 100 element.
  • 14.  Each element is defined to be of the type struct class.  An array of structure is stored inside the memory in the same way as a multi- dimensional array.
  • 15.  C permits the use of array as a structure members.  We can use single or multi-dimensional array of type int or float.  i.e. Struct marks { int number; float subject[3]; } student[2];
  • 16.  The main philosophy of c language is the use of functions. C supports the passing of structure values as arguments to function.  There are 3 methods by which the values of a structure can be transfferd from one function to another: 1. To pass each member of the structure as an argument of function call.
  • 17. 2. Passing of a copy of the entire structure to the called function 3. Pointers to pass the structure as an argument.  General formet of sending a copy of a structure to thr called function: data_type function name(st_name) srtuct_type st_name; { … return(expression); }
  • 18.  Unions are a concept borrowed from structures and therefore follow the same syntax as structures.  Major diffferance in terms of Storage: In structures each member has its own storage location In unions all members use the same location
  • 19.  Union may contain many members of different type but can handle only one member at a time.  It can be declared using keyword union as follows:  union item { int m; float x; char c; } code;
  • 20.  Union union_name { data_type member1; data_type mrmber2; … … .. ….. } var1, var2, .. ;
  • 21. Union book; { char title[15]; char *author; int pages; float price; } b1, b2, b3;
  • 22.  A union variable can be assigned to another union variable.  Address of the union variable is obtained using the address of ‘&’ operator.  It is to pass a union to function and a function can return a union.  We can use pointer to unions and within unions.
  • 23.  All members share the same storage area in computers memory.