SlideShare a Scribd company logo
Mrs. G. Nagalakshmi, M.Sc., M.Phil.,
Ms. N. Malathi, M.Sc.,
Programming in C (18UAMA41)
II B.Sc. Mathematics (SF)
A structure is a convenient tool for handling a group of
logically related data items. Structure help to organize complex
data is a more meaningful way. It is powerful concept that we may
after need to use in our program Design.
Example
Lets take the example of a book, if we cant to declare a book
we will be thinking about the name, title, authors and publisher of the
book and publishing year. So to declare a book we need to have some
complex data type which can deal with more than one data types.
This can be done with the help of Structure.
Syntax
We can define a structure by using the keyword struct followed by
list of member variables contained in curly braces
struct Book
{
char Name[100];
char Author[100];
char Publisher[80];
int Year;
};
The keyword struct defines a book, and each line within the braces
defines the elements of the Book. Now whenever we create an
instance of Book it will have all the elements of the structure i.e.
Name, Author, Publisher and Year.
We can declare the structure by using the following syntax.
Once the Structure_name is declared as new data type, then
variables of that type can be declared as:
We can declare the structure variable CProgrammingBook as
follows:
struct Book CProgrammingBook;
Declaration can also be done as follows:
struct Book
{
char Name[100];
char Author[100];
char Publisher[80];
int Year;
} CProgrammingBook;
Lets initialize the structure variable CProgrammingBook.
Book CProgrammingBook =
{
“Programming in ANSI C",
“E. Balagurusamy",
“Tata Mc Graw Hills",
2008
};
C
We can access all the members of a structure by adding a
period after the name of the structure variable name and then the
name of the field we want to access.
For example we want to change the Publish year from 2008 to 2012,
we will do it as
CProgrammingBook.Year = 2012;
If we want to store data of 50 C Programming books, we
would be required to use 50 different structure variables, from book1
to book50. To have 50 separate variable will be inconvenient.
This can be done easily as follows:
struct Book
{
char Name[100];
char Author[100];
char Publisher[80];
int Year;
} CProgrammingBook[50];
Structure with in a structure means nesting of structures.
Let us consider the following structure defined to store information
about the salary of employees.
struct salary
{
char name[20];
char department[10];
int basic_pay;
int dearness_allowance;
int city_allowance;
}employee;
This structure defines name, department, basic pay and 3 kinds of
allowance. we can group all the items related to allowance together
and declare them under a substructure are shown below:
struct salary
{
char name [20];
char department[10];
str uct
{
int dearness;
int hous_rent;
int city;
} allowance;
}employee;
The salary structure contains a member named allowance which
itself is a structure with 3 members. The members contained in the
inner, structure namely dearness, house_rent, and city can be
referred to as :
employee.allowance. dearness
employee. allowance. house_rent
employee. allowance. city
An inner-most member in a nested structure can be accessed
by chaining all the concerned. Structure variables (from outer-most
to inner-most) with the member using dot operator.
The following are invalid.
employee. allowance (actual member is missing)
employee. house_rent (inner structure variable is missing)
•A union in C programming is a user defined data type which may
hold members of different sizes and type.
•Union uses a single memory location to hold more than one
variables. However, only one of its members can be accessed at a
time and all other members will contain garbage values.
•The memory required to store a union variable is the memory
required for the largest element of the union.
•Both are user-defined data types used to store data of different
types as a single unit.
•Their members can be objects of any type, including other
structures and unions or arrays. A member can also consist of a bit
field.
•Both structures and unions support only assignment = and sizeof
operators. The two structures or unions in the assignment must
have the same members and member types.
•A structure or a union can be passed by value to functions and
returned by value by functions. The argument must have the same
type as the function parameter. A structure or union is passed by
value just like a scalar variable as a corresponding parameter.
•‘.’ operator is used for accessing members.
A bit field is a data structure used in computer programming.
It consists of a number of adjacent computer memory locations
which have been allocated to hold a sequence of bits, stored so that
any single bit or group of bits within the set can be addressed.
In C, we can state the size of our structure (struct) or union
members in the form of bits. This concept is to because of efficiently
utilizing the memory when you know that your amount of a field or
collection of fields is not going to exceed a specific limit or is in-
between the desired range.
Let us take a situation of C program, which contains a
number having TRUE/FALSE variables clustered together in
structure form name as decided:
struct
{
int haveTime;
int haveSpace;
} decided;
The above structure involves 8-bytes of memory space, but we
need to hold either 0 or 1 for each of the above variables.
When implementing such variables within a structure, you can
implicitly characterize the width of a variable that tells the compiler
to exercise only that specific number of bytes. The above program
can be written associating bit field as:
struct
{
int haveTime: 1;
int haveSpace: 1;
} decided;
The above program involves 4 bytes of memory space for
the status variable, but only 2 bits will be used for storing the
values.
So in our above example, the structure has occupied 4
bytes of memory for a decided variable, but only 2 bits will be
implemented for storing the values.
Variables that are defined using a width or size are called bit
fields. The format and syntax of bit-field declaration inside a
structure is as follows:
struct {
data - type[nameofmember]: width_of_Bit - field;
};
Programming in C

More Related Content

What's hot

Programming with matlab session 2
Programming with matlab session 2Programming with matlab session 2
Programming with matlab session 2
Infinity Tech Solutions
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn Hub
Learn Hub
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
rumanatasnim415
 
Bt0065, c programming and data structures
Bt0065, c programming and data structuresBt0065, c programming and data structures
Bt0065, c programming and data structuressmumbahelp
 
introduction to Data Structure and classification
 introduction to Data Structure and classification introduction to Data Structure and classification
introduction to Data Structure and classification
chauhankapil
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
shaibal sharif
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 
Bt0065 c programming and data structures - theory-de
Bt0065 c programming and data structures - theory-deBt0065 c programming and data structures - theory-de
Bt0065 c programming and data structures - theory-de
smumbahelp
 
User defined data type
User defined data typeUser defined data type
User defined data typeAmit Kapoor
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
tanmaymodi4
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
Ram Sagar Mourya
 
Data structures
Data structuresData structures
Data structures
MADHAVASAIYENDUVA
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Relational Model
Relational ModelRelational Model
Relational Model
A. S. M. Shafi
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Towards a New Data Modelling Architecture - Part 1
Towards a New Data Modelling Architecture - Part 1Towards a New Data Modelling Architecture - Part 1
Towards a New Data Modelling Architecture - Part 1JEAN-MICHEL LETENNIER
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
sunilchute1
 

What's hot (20)

Programming with matlab session 2
Programming with matlab session 2Programming with matlab session 2
Programming with matlab session 2
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn Hub
 
Lecture 3.mte 407
Lecture 3.mte 407Lecture 3.mte 407
Lecture 3.mte 407
 
Bt0065, c programming and data structures
Bt0065, c programming and data structuresBt0065, c programming and data structures
Bt0065, c programming and data structures
 
introduction to Data Structure and classification
 introduction to Data Structure and classification introduction to Data Structure and classification
introduction to Data Structure and classification
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Bt0065 c programming and data structures - theory-de
Bt0065 c programming and data structures - theory-deBt0065 c programming and data structures - theory-de
Bt0065 c programming and data structures - theory-de
 
User defined data type
User defined data typeUser defined data type
User defined data type
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Data structures
Data structuresData structures
Data structures
 
Structure in c
Structure in cStructure in c
Structure in c
 
Relational Model
Relational ModelRelational Model
Relational Model
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Towards a New Data Modelling Architecture - Part 1
Towards a New Data Modelling Architecture - Part 1Towards a New Data Modelling Architecture - Part 1
Towards a New Data Modelling Architecture - Part 1
 
Tree
TreeTree
Tree
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 

Similar to Programming in C

Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
Abhishekkumarsingh630054
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Structures
StructuresStructures
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
sangrampatil81
 
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
meharikiros2
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
Mehul Desai
 
struct and class deferences
 struct and class deferences struct and class deferences
struct and class deferences
Naseer Khan Noor
 
Structures
StructuresStructures
Structures
arshpreetkaur07
 
C structure and union
C structure and unionC structure and union
C structure and union
Thesis Scientist Private Limited
 

Similar to Programming in C (20)

Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Structures
StructuresStructures
Structures
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Data structures
Data structuresData structures
Data structures
 
struct and class deferences
 struct and class deferences struct and class deferences
struct and class deferences
 
Structures
StructuresStructures
Structures
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
 
C structure and union
C structure and unionC structure and union
C structure and union
 

More from MalathiNagarajan20

Dynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal DistanceDynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal Distance
MalathiNagarajan20
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 
Dynamics problems
Dynamics problemsDynamics problems
Dynamics problems
MalathiNagarajan20
 
Dynamics ppt
Dynamics pptDynamics ppt
Dynamics ppt
MalathiNagarajan20
 
Numerical analysis ppt
Numerical analysis pptNumerical analysis ppt
Numerical analysis ppt
MalathiNagarajan20
 
C
CC
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)
MalathiNagarajan20
 
Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)
MalathiNagarajan20
 
Dynamics (18 umtc41)
Dynamics (18 umtc41)Dynamics (18 umtc41)
Dynamics (18 umtc41)
MalathiNagarajan20
 
Statics
StaticsStatics
Transforms
TransformsTransforms
Transforms
MalathiNagarajan20
 
Transforms
TransformsTransforms
Transforms
MalathiNagarajan20
 
Statics
StaticsStatics

More from MalathiNagarajan20 (13)

Dynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal DistanceDynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal Distance
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Dynamics problems
Dynamics problemsDynamics problems
Dynamics problems
 
Dynamics ppt
Dynamics pptDynamics ppt
Dynamics ppt
 
Numerical analysis ppt
Numerical analysis pptNumerical analysis ppt
Numerical analysis ppt
 
C
CC
C
 
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)
 
Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)
 
Dynamics (18 umtc41)
Dynamics (18 umtc41)Dynamics (18 umtc41)
Dynamics (18 umtc41)
 
Statics
StaticsStatics
Statics
 
Transforms
TransformsTransforms
Transforms
 
Transforms
TransformsTransforms
Transforms
 
Statics
StaticsStatics
Statics
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Programming in C

  • 1. Mrs. G. Nagalakshmi, M.Sc., M.Phil., Ms. N. Malathi, M.Sc., Programming in C (18UAMA41) II B.Sc. Mathematics (SF)
  • 2.
  • 3. A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.
  • 4. Example Lets take the example of a book, if we cant to declare a book we will be thinking about the name, title, authors and publisher of the book and publishing year. So to declare a book we need to have some complex data type which can deal with more than one data types. This can be done with the help of Structure.
  • 6. We can define a structure by using the keyword struct followed by list of member variables contained in curly braces struct Book { char Name[100]; char Author[100]; char Publisher[80]; int Year; }; The keyword struct defines a book, and each line within the braces defines the elements of the Book. Now whenever we create an instance of Book it will have all the elements of the structure i.e. Name, Author, Publisher and Year.
  • 7. We can declare the structure by using the following syntax. Once the Structure_name is declared as new data type, then variables of that type can be declared as:
  • 8. We can declare the structure variable CProgrammingBook as follows: struct Book CProgrammingBook; Declaration can also be done as follows: struct Book { char Name[100]; char Author[100]; char Publisher[80]; int Year; } CProgrammingBook;
  • 9. Lets initialize the structure variable CProgrammingBook. Book CProgrammingBook = { “Programming in ANSI C", “E. Balagurusamy", “Tata Mc Graw Hills", 2008 }; C
  • 10. We can access all the members of a structure by adding a period after the name of the structure variable name and then the name of the field we want to access. For example we want to change the Publish year from 2008 to 2012, we will do it as CProgrammingBook.Year = 2012;
  • 11. If we want to store data of 50 C Programming books, we would be required to use 50 different structure variables, from book1 to book50. To have 50 separate variable will be inconvenient. This can be done easily as follows: struct Book { char Name[100]; char Author[100]; char Publisher[80]; int Year; } CProgrammingBook[50];
  • 12. Structure with in a structure means nesting of structures. Let us consider the following structure defined to store information about the salary of employees. struct salary { char name[20]; char department[10]; int basic_pay; int dearness_allowance; int city_allowance; }employee;
  • 13. This structure defines name, department, basic pay and 3 kinds of allowance. we can group all the items related to allowance together and declare them under a substructure are shown below: struct salary { char name [20]; char department[10]; str uct { int dearness; int hous_rent; int city; } allowance; }employee;
  • 14. The salary structure contains a member named allowance which itself is a structure with 3 members. The members contained in the inner, structure namely dearness, house_rent, and city can be referred to as : employee.allowance. dearness employee. allowance. house_rent employee. allowance. city
  • 15. An inner-most member in a nested structure can be accessed by chaining all the concerned. Structure variables (from outer-most to inner-most) with the member using dot operator. The following are invalid. employee. allowance (actual member is missing) employee. house_rent (inner structure variable is missing)
  • 16. •A union in C programming is a user defined data type which may hold members of different sizes and type. •Union uses a single memory location to hold more than one variables. However, only one of its members can be accessed at a time and all other members will contain garbage values. •The memory required to store a union variable is the memory required for the largest element of the union.
  • 17. •Both are user-defined data types used to store data of different types as a single unit. •Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field. •Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types. •A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter. •‘.’ operator is used for accessing members.
  • 18.
  • 19. A bit field is a data structure used in computer programming. It consists of a number of adjacent computer memory locations which have been allocated to hold a sequence of bits, stored so that any single bit or group of bits within the set can be addressed.
  • 20. In C, we can state the size of our structure (struct) or union members in the form of bits. This concept is to because of efficiently utilizing the memory when you know that your amount of a field or collection of fields is not going to exceed a specific limit or is in- between the desired range.
  • 21. Let us take a situation of C program, which contains a number having TRUE/FALSE variables clustered together in structure form name as decided: struct { int haveTime; int haveSpace; } decided; The above structure involves 8-bytes of memory space, but we need to hold either 0 or 1 for each of the above variables.
  • 22. When implementing such variables within a structure, you can implicitly characterize the width of a variable that tells the compiler to exercise only that specific number of bytes. The above program can be written associating bit field as: struct { int haveTime: 1; int haveSpace: 1; } decided;
  • 23. The above program involves 4 bytes of memory space for the status variable, but only 2 bits will be used for storing the values. So in our above example, the structure has occupied 4 bytes of memory for a decided variable, but only 2 bits will be implemented for storing the values.
  • 24. Variables that are defined using a width or size are called bit fields. The format and syntax of bit-field declaration inside a structure is as follows: struct { data - type[nameofmember]: width_of_Bit - field; };