SlideShare a Scribd company logo
PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C
PAPER ID: 20105
PAPER CODE: BCA 105
DR. VARUN TIWARI
(ASSOCIATE PROFESSOR)
(DEPARTMENT OF COMPUTER SCIENCE)
BOSCO TECHNICAL TRAINING SOCIETY,
DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
C STRUCTURE
AND UNION
OBJECTIVES
IN THIS CHAPTER YOU WILL LEARN:
1. TO UNDERSTAND ABOUT STRUCTURE IN C.
2. TO LEARN ABOUT HOW TO DECLARE STRUCTURE IN C.
3. TO LEARN ABOUT HOW TO STORE STRUCTURE IN MEMORY.
4. TO UNDERSTAND COPY OF STRUCTURE ELEMENTS IN C.
5. TO UNDERSTAND ABOUT NESTED STRUCTURE IN C.
6. TO LEARN ABOUT HOW TO USE ARRAY OF STRUCTURE IN C.
7. TO LEARN ABOUT UNION IN C.
STRUCTURE: IT IS REQUIRED TO STORE GROUP OF ALL INFORMATION INTO ONE VARIABLE.
PROGRAMMER CAN USE AN ARRAY TO STORE A GROUP OF DATA ITEM THAT BELONG TO THE SAME
TYPE. BUT IF IT IS REQUIRED TO REPRESENT A COLLECTION OF DATA ITEMS OF DIFFERENT TYPES USING
A SINGLE NAME THEN BE CAN’T WE AN ARRAY. C LANGUAGE PROVIDE A METHOD OF GROUPING ALL
OF THE INFORMATION IN TO ONE VARIABLE IS CALLED A STRUCTURE. A STRUCTURE IS SIMILAR TO A
RECORD. A RECORD CREATED AS A GROUP OF DATA WHICH NEED TO BE PROCESS AS A GROUP FOR
EXAMPLE: FOR ONE STUDENT PROGRAMMER MAY NEED TO BE STORE DATA SUCH AS ROLLNO , NAME
, SUBJECT ,MARKS, TOTAL AND PERCENTAGE. SUPPOSE YOU WANT TO STORE DATA ABOUT A BOOK.
YOU MIGHT WANT TO STORE ITS NAME (STRING),PRICE (FLOAT), NOOFPAGE(INT). IF DATA ABOUT 3
SUCH BOOKS TO BE STORED, THEN WE CAN FOLLOW TWO APPROACH:
1. CONSTRUCT INDIVIDUAL ARRAYS, ONE FOR STORING NAME , ANOTHER FOR STORING PRICE AND
STILL ANOTHER FOR STORING PAGES.
2. USE A STRUCTURE.
EXAMPLE OF FIRST APPROACH:
#include<conio.h>
#include<stdio.h>
void main() {
char name[3]; float price[3];
int page[3],i;
printf("n enter name, price and page of 3 books");
for(i=0;i<=2;i++) {
scanf("%s%f%d",&name[i],&price[i],&page[i]); }
printf("n output value is -:");
for(i=0;i<=2;i++) {
printf("n output is -: %c %f %d",name[i],price[i],page[i]); }
getch(); }
EXAMPLE OF SECOND APPROACH:
void main() {
struct book{
char name; float price;
int page; };struct book b1,b2,b3;
clrscr();
printf("n enter name, price and page of 3 books");
scanf("n %c %f %d",&b1.name,&b1.price,&b1.page);
scanf("n %c %f %d",&b2.name,&b2.price,&b2.page);
scanf("n %c %f %d",&b3.name,&b3.price,&b3.page);
printf("n output is -:");
printf("n output is -:%c %f %d",b1.name,b1.price,b1.page);
printf("n output is-: %c %f %d",b2.name,b2.price,b2.page);
printf("n output is-: %c %f %d",b3.name,b3.price,b3.page);
getch(); }
STRUCTURE DECLARATION: THIS PROGRAM DEMONSTRATE TWO FUNDAMENTAL ASPECTS OF STRUCTURE.
A). DECLARATION OF STRUCTURE
B) ACCESSING OF STRUCTURE ELEMENT
A) DECLARATION OF STRUCTURE: FOR CREATING A STRUCTURE BE USE STRUCT STATEMENT. THE GENERAL SYNTAX TO CREATE STRUCTURE.
STRUCT <TAGNAME>
{
DATATYPE1 MEMBER VARIABLE1
DATATYPE2 MEMBER VARIABLE2
DATATYPE3 MEMBER VARIABLE3
DATATYPE N MEMBER VARIABLE
};
EX: STRUCT BOOK
{
CHAR P[20];
INT ROLLNO;
FLOAT FEES;
};
B) ACCESSING OF STRUCTURE ELEMENT: WE CAN EASILY ACCESS MEMBER VARIABLE BY USING
STRUCTURE VARIABLE:
EX: STRUCT STD
{
INT A;
INT B;
CHAR P1[20];
}A1,B1,A2; //TYPE –I STRUCTURE VARIABLE DECLARATION
STRUCT STD P1,P2,P3; //TYPE – II STRUCTURE VARIABLE DECLARATION
STORAGE OF STRUCTURE IN MEMORY: STRUCTURE ELEMENTS ARE ALWAYS STORED IN CONTIGUOUS
MEMORY LOCATIONS. EXAMPLE IS-:
void main() {
struct book {
char name;
float price;
int pages; };
struct book b1={'b',130.00,550};
clrscr();
printf("n address of name %u",&b1.name);
printf("n address of price -: %u",&b1.price);
printf("n address of pages -%u",&b1.pages);
getch(); }
STORAGE OF STRUCTURE IN MEMORY:
COPY OF STRUCTURE ELEMENT IN C: STRUCTURE ELEMENTS CAN BE COPIED EITHER PIECE-MEAL OR ALL AT ONE SHOT. BOTH
THESE APPROACHES ARE SHOWN IN THE FOLLOWING EXAMPLE:
void main() { struct book {
char name[10]; float price; int pages; };
struct book b1={"vivek",123.34,550}; struct book b2,b3;
clrscr();
strcpy(b2.name,b1.name);
b2.price=b1.price;
b2.pages=b1.pages;
b3=b2; //copying structure variable
printf("n %s n %f n %d",b1.name,b1.price,b1.pages);
printf("n %s n %f n %d",b2.name,b2.price,b2.pages);
printf("n %s n %f n %d",b3.name,b3.price,b3.pages);
getch(); }
NESTED STRUCTURE IN C: ONE STRUCTURE CAN BE NESTED WITHIN ANOTHER STRUCTURE. USING THIS
FACILITY , COMPLEX DATA TYPES CAN BE CREATED. THE FOLLOWING PROGRAM SHOWN NESTED
STRUCTURE AT WORK:
void main() {
struct student {
char name[20],subject[20];int age; };
struct book {
char bname[20]; int bid;
struct student e; };
struct book b1={"let us c",101,"vivek","C programming",10};
clrscr();
printf("n bname=%s n bid=%d name=%s n subject=%s n
age=%d",b1.bname,b1.bid,b1.e.name,b1.e.subject,b1.e.age); getch();
}
ARRAY OF STRUCTURE IN C: WE KNOW THAT WE CAN CREATE ARRAY WITH IN STRUCTURE & WE CAN ALSO
CREATE STRUCTURE VARIABLE AS ARRAY. IN WHICH WE CAN STORE NO OF RECORDS IN ROWS. IF YOU ARE
CREATING STRUCTURE VARIABLE OF ARRAY THAT YOU DON’T HAVE TO CREATE ALL STRUCTURE VARIABLE
AS ARRAY EXCEPT CHARACTER.
EX: void main() {
struct stud{ char name[20]; int age; }b1[3];//array of structure
int i; clrscr();
for(i=0;i<2;i++) {
printf("n enter student name-:");
scanf("%s",&b1[i].name);
printf("n enter age-:");
scanf("%d",&b1[i].age);}
for(i=0;i<2;i++) {
printf("n name is-: %s",b1[i].name);
printf("n age is -: %d",b1[i].age); }
getch(); }
Union in C: union are derived data types , the way structures are : union and structure look alike , but are
arranged in totally different activity.
A structure enables us treat a number of different variables stored at different places in memory. Unlike
this a union enables us to treat the same space in memory as a number of different variables.
#include<conio.h> #include<stdio.h>
void main() { union book {
int bid; float p; } b1;
struct book1 { int bid1; float p1; } b2;
clrscr(); printf("n Enter two number first for union and second for structure-:");
scanf("%d%d",&b1.bid,&b2.bid1);
printf("n %dn %d",b1.bid,b2.bid1);
printf("n Enter two float value first union and second structure-:");
scanf("%f%f",&b1.p,&b2.p1);
printf("n %fn %f",b1.p,b2.p1);
getch(); }
STORAGE OF UNION IN MEMORY:
THANK YOU
Ad

More Related Content

What's hot (20)

Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
Hattori Sidek
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
baabtra.com - No. 1 supplier of quality freshers
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Function recap
Function recapFunction recap
Function recap
alish sha
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Function basics
Function basicsFunction basics
Function basics
mohamed sikander
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
Utsav276
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2
karmuhtam
 
Ch6 pointers (latest)
Ch6 pointers (latest)Ch6 pointers (latest)
Ch6 pointers (latest)
Hattori Sidek
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Pointers in c
Pointers in cPointers in c
Pointers in c
guestb811fb
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
Dr. SURBHI SAROHA
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Pointers in C
Pointers in CPointers in C
Pointers in C
guestdc3f16
 
Pointer in C
Pointer in CPointer in C
Pointer in C
Sonya Akter Rupa
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Function recap
Function recapFunction recap
Function recap
alish sha
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
Utsav276
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2
karmuhtam
 
Ch6 pointers (latest)
Ch6 pointers (latest)Ch6 pointers (latest)
Ch6 pointers (latest)
Hattori Sidek
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 

Similar to C Structure and Union in C (20)

Data struture lab
Data struture labData struture lab
Data struture lab
krishnamurthy Murthy.Tt
 
Principals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdfPrincipals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdf
anilcsbs
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
Sheik Mohideen
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory AllocationPointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
03 structures
03 structures03 structures
03 structures
Rajan Gautam
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
Asfand Hassan
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
YOGESH SINGH
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
thenmozhip8
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
Kongunadu College of Engineering and Technology
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
vinay arora
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
C++ Language
C++ LanguageC++ Language
C++ Language
Vidyacenter
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
Animesh Chaturvedi
 
Computer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdfComputer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdf
sujathachoudaryn29
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
some basic C programs with outputs
some basic C programs with outputssome basic C programs with outputs
some basic C programs with outputs
KULDEEPSING PATIL
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
Bianca Teşilă
 
Ch 4
Ch 4Ch 4
Ch 4
AMIT JAIN
 
Ad

More from Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi) (20)

Preprocessor Directive in C
Preprocessor Directive in CPreprocessor Directive in C
Preprocessor Directive in C
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Bit field enum and command line arguments
Bit field enum and command line argumentsBit field enum and command line arguments
Bit field enum and command line arguments
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Array in C
Array in CArray in C
Array in C
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C storage class
C storage classC storage class
C storage class
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C Operators
C OperatorsC Operators
C Operators
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C programming Basics
C programming BasicsC programming Basics
C programming Basics
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Software Development Skills and SDLC
Software Development Skills and SDLCSoftware Development Skills and SDLC
Software Development Skills and SDLC
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Mobile commerce
Mobile commerceMobile commerce
Mobile commerce
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
E commerce application
E commerce applicationE commerce application
E commerce application
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Data normalization
Data normalizationData normalization
Data normalization
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Html Form Controls
Html Form ControlsHtml Form Controls
Html Form Controls
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Security issue in e commerce
Security issue in e commerceSecurity issue in e commerce
Security issue in e commerce
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
ER to Relational Mapping
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Entity Relationship Model
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Ado vs ado.net by varun tiwari
Ado vs ado.net by varun tiwariAdo vs ado.net by varun tiwari
Ado vs ado.net by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Client server architecture in .net by varun tiwari
Client server architecture in .net by varun tiwariClient server architecture in .net by varun tiwari
Client server architecture in .net by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Ad

Recently uploaded (20)

LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 

C Structure and Union in C

  • 1. PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C PAPER ID: 20105 PAPER CODE: BCA 105 DR. VARUN TIWARI (ASSOCIATE PROFESSOR) (DEPARTMENT OF COMPUTER SCIENCE) BOSCO TECHNICAL TRAINING SOCIETY, DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
  • 3. OBJECTIVES IN THIS CHAPTER YOU WILL LEARN: 1. TO UNDERSTAND ABOUT STRUCTURE IN C. 2. TO LEARN ABOUT HOW TO DECLARE STRUCTURE IN C. 3. TO LEARN ABOUT HOW TO STORE STRUCTURE IN MEMORY. 4. TO UNDERSTAND COPY OF STRUCTURE ELEMENTS IN C. 5. TO UNDERSTAND ABOUT NESTED STRUCTURE IN C. 6. TO LEARN ABOUT HOW TO USE ARRAY OF STRUCTURE IN C. 7. TO LEARN ABOUT UNION IN C.
  • 4. STRUCTURE: IT IS REQUIRED TO STORE GROUP OF ALL INFORMATION INTO ONE VARIABLE. PROGRAMMER CAN USE AN ARRAY TO STORE A GROUP OF DATA ITEM THAT BELONG TO THE SAME TYPE. BUT IF IT IS REQUIRED TO REPRESENT A COLLECTION OF DATA ITEMS OF DIFFERENT TYPES USING A SINGLE NAME THEN BE CAN’T WE AN ARRAY. C LANGUAGE PROVIDE A METHOD OF GROUPING ALL OF THE INFORMATION IN TO ONE VARIABLE IS CALLED A STRUCTURE. A STRUCTURE IS SIMILAR TO A RECORD. A RECORD CREATED AS A GROUP OF DATA WHICH NEED TO BE PROCESS AS A GROUP FOR EXAMPLE: FOR ONE STUDENT PROGRAMMER MAY NEED TO BE STORE DATA SUCH AS ROLLNO , NAME , SUBJECT ,MARKS, TOTAL AND PERCENTAGE. SUPPOSE YOU WANT TO STORE DATA ABOUT A BOOK. YOU MIGHT WANT TO STORE ITS NAME (STRING),PRICE (FLOAT), NOOFPAGE(INT). IF DATA ABOUT 3 SUCH BOOKS TO BE STORED, THEN WE CAN FOLLOW TWO APPROACH: 1. CONSTRUCT INDIVIDUAL ARRAYS, ONE FOR STORING NAME , ANOTHER FOR STORING PRICE AND STILL ANOTHER FOR STORING PAGES. 2. USE A STRUCTURE.
  • 5. EXAMPLE OF FIRST APPROACH: #include<conio.h> #include<stdio.h> void main() { char name[3]; float price[3]; int page[3],i; printf("n enter name, price and page of 3 books"); for(i=0;i<=2;i++) { scanf("%s%f%d",&name[i],&price[i],&page[i]); } printf("n output value is -:"); for(i=0;i<=2;i++) { printf("n output is -: %c %f %d",name[i],price[i],page[i]); } getch(); }
  • 6. EXAMPLE OF SECOND APPROACH: void main() { struct book{ char name; float price; int page; };struct book b1,b2,b3; clrscr(); printf("n enter name, price and page of 3 books"); scanf("n %c %f %d",&b1.name,&b1.price,&b1.page); scanf("n %c %f %d",&b2.name,&b2.price,&b2.page); scanf("n %c %f %d",&b3.name,&b3.price,&b3.page); printf("n output is -:"); printf("n output is -:%c %f %d",b1.name,b1.price,b1.page); printf("n output is-: %c %f %d",b2.name,b2.price,b2.page); printf("n output is-: %c %f %d",b3.name,b3.price,b3.page); getch(); }
  • 7. STRUCTURE DECLARATION: THIS PROGRAM DEMONSTRATE TWO FUNDAMENTAL ASPECTS OF STRUCTURE. A). DECLARATION OF STRUCTURE B) ACCESSING OF STRUCTURE ELEMENT A) DECLARATION OF STRUCTURE: FOR CREATING A STRUCTURE BE USE STRUCT STATEMENT. THE GENERAL SYNTAX TO CREATE STRUCTURE. STRUCT <TAGNAME> { DATATYPE1 MEMBER VARIABLE1 DATATYPE2 MEMBER VARIABLE2 DATATYPE3 MEMBER VARIABLE3 DATATYPE N MEMBER VARIABLE }; EX: STRUCT BOOK { CHAR P[20]; INT ROLLNO; FLOAT FEES; };
  • 8. B) ACCESSING OF STRUCTURE ELEMENT: WE CAN EASILY ACCESS MEMBER VARIABLE BY USING STRUCTURE VARIABLE: EX: STRUCT STD { INT A; INT B; CHAR P1[20]; }A1,B1,A2; //TYPE –I STRUCTURE VARIABLE DECLARATION STRUCT STD P1,P2,P3; //TYPE – II STRUCTURE VARIABLE DECLARATION
  • 9. STORAGE OF STRUCTURE IN MEMORY: STRUCTURE ELEMENTS ARE ALWAYS STORED IN CONTIGUOUS MEMORY LOCATIONS. EXAMPLE IS-: void main() { struct book { char name; float price; int pages; }; struct book b1={'b',130.00,550}; clrscr(); printf("n address of name %u",&b1.name); printf("n address of price -: %u",&b1.price); printf("n address of pages -%u",&b1.pages); getch(); }
  • 10. STORAGE OF STRUCTURE IN MEMORY:
  • 11. COPY OF STRUCTURE ELEMENT IN C: STRUCTURE ELEMENTS CAN BE COPIED EITHER PIECE-MEAL OR ALL AT ONE SHOT. BOTH THESE APPROACHES ARE SHOWN IN THE FOLLOWING EXAMPLE: void main() { struct book { char name[10]; float price; int pages; }; struct book b1={"vivek",123.34,550}; struct book b2,b3; clrscr(); strcpy(b2.name,b1.name); b2.price=b1.price; b2.pages=b1.pages; b3=b2; //copying structure variable printf("n %s n %f n %d",b1.name,b1.price,b1.pages); printf("n %s n %f n %d",b2.name,b2.price,b2.pages); printf("n %s n %f n %d",b3.name,b3.price,b3.pages); getch(); }
  • 12. NESTED STRUCTURE IN C: ONE STRUCTURE CAN BE NESTED WITHIN ANOTHER STRUCTURE. USING THIS FACILITY , COMPLEX DATA TYPES CAN BE CREATED. THE FOLLOWING PROGRAM SHOWN NESTED STRUCTURE AT WORK: void main() { struct student { char name[20],subject[20];int age; }; struct book { char bname[20]; int bid; struct student e; }; struct book b1={"let us c",101,"vivek","C programming",10}; clrscr(); printf("n bname=%s n bid=%d name=%s n subject=%s n age=%d",b1.bname,b1.bid,b1.e.name,b1.e.subject,b1.e.age); getch(); }
  • 13. ARRAY OF STRUCTURE IN C: WE KNOW THAT WE CAN CREATE ARRAY WITH IN STRUCTURE & WE CAN ALSO CREATE STRUCTURE VARIABLE AS ARRAY. IN WHICH WE CAN STORE NO OF RECORDS IN ROWS. IF YOU ARE CREATING STRUCTURE VARIABLE OF ARRAY THAT YOU DON’T HAVE TO CREATE ALL STRUCTURE VARIABLE AS ARRAY EXCEPT CHARACTER. EX: void main() { struct stud{ char name[20]; int age; }b1[3];//array of structure int i; clrscr(); for(i=0;i<2;i++) { printf("n enter student name-:"); scanf("%s",&b1[i].name); printf("n enter age-:"); scanf("%d",&b1[i].age);} for(i=0;i<2;i++) { printf("n name is-: %s",b1[i].name); printf("n age is -: %d",b1[i].age); } getch(); }
  • 14. Union in C: union are derived data types , the way structures are : union and structure look alike , but are arranged in totally different activity. A structure enables us treat a number of different variables stored at different places in memory. Unlike this a union enables us to treat the same space in memory as a number of different variables. #include<conio.h> #include<stdio.h> void main() { union book { int bid; float p; } b1; struct book1 { int bid1; float p1; } b2; clrscr(); printf("n Enter two number first for union and second for structure-:"); scanf("%d%d",&b1.bid,&b2.bid1); printf("n %dn %d",b1.bid,b2.bid1); printf("n Enter two float value first union and second structure-:"); scanf("%f%f",&b1.p,&b2.p1); printf("n %fn %f",b1.p,b2.p1); getch(); }
  • 15. STORAGE OF UNION IN MEMORY: