SlideShare a Scribd company logo
1 of 49
Download to read offline
STRUCTURES
AND
UNIONS
DoCSE
Introduction
Defining a Structure
Contd…
Example
A Compact Form
#include<stdio.h>
typedef struct{
char name[80];
char dept[30];
int roll_no;
} studinfo;
Size of a Structure
main()
{
studinfo a;
printf("a store %d bytes",sizeof(a));
}
struct date {
char name[80];
int month;
int day;
int year;
}
struct date birthday[ ] = {“Amy”, 12, 30, 73, “Gail”, 5, 13, 66, “Marc”, 7, 15,
72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,
12,69};
#include<stdio.h>
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
main()
{
struct studinfo si;
printf("Your name:");printf("Your name:");
scanf(“ %[^n]",si.name);
printf("nYour department:");
scanf("%s",si.dept);
printf("nYour roll number:");
scanf("%d",&si.roll_no);
printf("*************************");
printf("nYour Name:%s",si.name);
printf("nYour Department:%s",si.dept);
printf("nYour Roll Number:%d",si.roll_no);
}
#include<stdio.h>
struct date{
int month;
int day;
int year;
};
struct studinfo{
char name[80];
char dept[30];
int roll_no;
main( )
{
struct studinfo si;
printf("Your name:");
scanf(“ %[^n]",si.name);
printf("nYour department:");
scanf("%s",si.dept);
printf("nYour roll number:");
scanf("%d",&si.roll_no);
printf("nDate of birth:(mm/dd/yy)");
scanf("%d%d%d",&si.birth.month,&si.birth.day,&si.birth.year);
printf("*****************************");
printf("nYour Name:%s",si.name);
printf("nYour Department:%s",si.dept);
printf("nYour Roll Number:%d",si.roll_no);
printf("nYour Birthday:%d-%d-
int roll_no;
struct date birth;
};
printf("nYour Birthday:%d-%d-
%d",si.birth.month,si.birth.day,si.birth.year);
}
#include<stdio.h>
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
main()
{
int n,i;
struct studinfo si[100];
printf("*****************************n");
printf("******************************");
printf("n");
for(i=0;i<n;i++)
{
printf("nYour Name:%s",si[i].name);
printf("nYour Department:%s",si[i].dept);
printf("nYour Roll Number:%d",si[i].roll_no);
printf("n************************************");
}
}
struct studinfo si[100];
printf("How many student
information are you storing: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Your name:");
scanf(“ %[^n]",si[i].name);
printf("Your department:");
scanf(“ %[^n]",si[i].dept);
printf("Your roll number:");
scanf("%d",&si[i].roll_no);
}
#include<stdio.h>
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
main()
{
int n,i;
struct studinfo si[100];
printf("*****************************n");
printf("******************************");
printf("n");
for(i=0;i<n;i++)
{
printf("nYour Name:%s",si[i].name);
printf("nYour Department:%s",si[i].dept);
printf("nYour Roll Number:%d",si[i].roll_no);
printf("n************************************");
}
}
struct studinfo si[100];
printf("How many student
information are you storing: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Your name:");
scanf(“ %[^n]",si[i].name);
printf("Your department:");
scanf(“ %[^n]",si[i].dept);
printf("Your roll number:");
scanf("%d",&si[i].roll_no);
}
#include<stdio.h>
struct date{
int month;
int day;
int year;
};
struct studinfo{
char name[80];
char dept[30];
main()
{
int n,i;
struct studinfo si[100];
printf("How many student information are you
storing: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Your name:");
scanf(“ %[^n]",si[i].name);
printf("Your department:");char dept[30];
int roll_no;
struct date birth;
};
printf("Your department:");
fflush(stdin);
scanf(“ %[^n]",si[i].dept);
printf("Your roll number:");
scanf("%d",&si[i].roll_no);
printf("nDate of birth:(mm/dd/yy)");
scanf("%d%d%d",&si[i].birth.month,&si[i].birth.d
ay,&si[i].birth.year);
}
Processing a Structure
Programming Example
Comparision of structure variable
Arrays of Structure
Defining date type: using typedef
Structure Initialization
Parameter Passing in a function
Returning Structure
Array of Structure
Example: Sorting
Pointer and Structure
Example: Pointer
Things to remember
Contd…
storage-class struct tag {
member 1 ;
member 2;
. . . . .
member m;member m;
}variable 1, variable 2, . . ., variable n;
it is possible to combine the declaration of the structure
composition with that of the structure variables
Contd…
struct account {
int acct_no;
char acct_type;
char name[80];
float balance ;float balance ;
} oldcustomer, newcustomer;
oldcustomer and newcustomer are structure variables of
type account
Contd…
struct date {
int month;
int day;
int year;
};
struct account {
int acct_no;
char acct_type;
char name[80];
float balance ;
struct date lastpayment;
};
Contd…
struct account customer
= {12345, ‘R’, “John W. Smith”, 586.30, 5, 24, 90};
//structure name account
//structure variable customer
Members: acct_no = 12345; acct_type= ‘R’; name[80] = “John W.
Smith”; balance = 586.30; month = 5; day = 24; year = 90
Declaration: Structure Variable as an array
struct date {
int month;
int day;
int year;
};
struct account
{
int acct-no;
struct date {
int month;
int day;
int year;
} ;
struct account {
int acct_no;
char acct-type;int acct-no;
char acct-type;
char name[80];
float balance;
struct date lastpayment;
} customer[ 100];
char acct-type;
char name[80];
float balance ;
struct date lastpayment;
} ;
struct account customer[100];
struct date {
char name[80];
int month;
int day;
int year;
}
struct date birthday[ ] = {“Amy”, 12, 30, 73, “Gail”, 5, 13, 66, “Marc”, 7, 15,
72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,
12,69};
#include<stdio.h>
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
main()
{
struct studinfo si;
printf("Your name:");printf("Your name:");
scanf(“ %[^n]",si.name);
printf("nYour department:");
scanf("%s",si.dept);
printf("nYour roll number:");
scanf("%d",&si.roll_no);
printf("*************************");
printf("nYour Name:%s",si.name);
printf("nYour Department:%s",si.dept);
printf("nYour Roll Number:%d",si.roll_no);
}
#include<stdio.h>
struct date{
int month;
int day;
int year;
};
struct studinfo{
char name[80];
char dept[30];
int roll_no;
main( )
{
struct studinfo si;
printf("Your name:");
scanf(“ %[^n]",si.name);
printf("nYour department:");
scanf("%s",si.dept);
printf("nYour roll number:");
scanf("%d",&si.roll_no);
printf("nDate of birth:(mm/dd/yy)");
scanf("%d%d%d",&si.birth.month,&si.birth.day,&si.birth.year);
printf("*****************************");
printf("nYour Name:%s",si.name);
printf("nYour Department:%s",si.dept);
printf("nYour Roll Number:%d",si.roll_no);
printf("nYour Birthday:%d-%d-
int roll_no;
struct date birth;
};
printf("nYour Birthday:%d-%d-
%d",si.birth.month,si.birth.day,si.birth.year);
}
#include<stdio.h>
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
main()
{
int n,i;
struct studinfo si[100];
printf("*****************************n");
printf("******************************");
printf("n");
for(i=0;i<n;i++)
{
printf("nYour Name:%s",si[i].name);
printf("nYour Department:%s",si[i].dept);
printf("nYour Roll Number:%d",si[i].roll_no);
printf("n************************************");
}
}
struct studinfo si[100];
printf("How many student
information are you storing: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Your name:");
scanf(“ %[^n]",si[i].name);
printf("Your department:");
scanf(“ %[^n]",si[i].dept);
printf("Your roll number:");
scanf("%d",&si[i].roll_no);
}
#include<stdio.h>
struct date{
int month;
int day;
int year;
};
struct studinfo{
char name[80];
char dept[30];
main()
{
int n,i;
struct studinfo si[100];
printf("How many student information are you
storing: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Your name:");
scanf(“ %[^n]",si[i].name);
printf("Your department:");char dept[30];
int roll_no;
struct date birth;
};
printf("Your department:");
fflush(stdin);
scanf(“ %[^n]",si[i].dept);
printf("Your roll number:");
scanf("%d",&si[i].roll_no);
printf("nDate of birth:(mm/dd/yy)");
scanf("%d%d%d",&si[i].birth.month,&si[i].birth.d
ay,&si[i].birth.year);
}
printf("*****************************n");
printf("******************************");
printf("n");
for(i=0;i<n;i++){
printf("nYour Name:%s",si[i].name);
printf("nYour Department:%s",si[i].dept);
printf("nYour Roll Number:%d",si[i].roll_no);
printf("nYour Birthday:%d-%d-
%d",si[i].birth.month,si[i].birth.day,si[i].birth.year);
printf("n************************************");printf("n************************************");
}
}
typedef
• typedef feature allows users to define
new data-types that are equivalent to
existing data types
• new data type is defined as
typedef type new- type;
typedef int age;
//Example
#include<stdio.h>
main()
{
typedef int sk;
sk a,b,sum;
printf("Enter two number");
scanf("%d%d",&a,&b);typedef int age;
age male, female;
is equivalent to writing
int male, female;
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum of %d and %d is
%d",a,b,sum);
}
Contd…
struct studinfo{
char name[80];
char dept[30];
int roll_no;
};
struct studinfo a,b;
typedef studinfo{
char name[80];
char dept[30];
int roll_no;
} studinfo;
studinfo a,*pa;
#include<stdio.h>
typedef struct{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a,*pa;
pa=&a;
printf("What is your name? ");
scanf(" %[^n]",a.name);
#include<stdio.h>
typedef struct{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a,*pa;
pa=&a;
printf("What is your name? ");scanf(" %[^n]",a.name);
printf("n Your department ");
scanf(" %[^n]",a.dept);
printf("n Your Roll number ");
scanf("%d",&a.roll_no);
printf("n***********************n");
printf("Your Name:%s",pa->name);
printf("nYour Department:%s",pa-
>dept);
printf("n Your Roll no:%d",pa->roll_no);
}
printf("What is your name? ");
scanf(" %[^n]",a.name);
printf("n Your department ");
scanf(" %[^n]",a.dept);
printf("n Your Roll number ");
scanf("%d",&a.roll_no);
printf("n***********************n");
printf("Your Name:%s",(*pa).name);
printf("nYour Department:%s",(*pa).dept);
printf("n Your Roll no:%d",(*pa).roll_no);
}
#include<stdio.h>
typedef struct{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a,*pa;
pa=&a;
printf("What is your name? ");
scanf(" %[^n]",a.name);
printf("n Your department ");
scanf(" %[^n]",a.dept);
#include<stdio.h>
int update(int n)
{
n=n+1;
return n;
}
typedef struct{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a,*pa;scanf(" %[^n]",a.dept);
printf("n Your Roll number ");
scanf("%d",&a.roll_no);
printf("n***********************n");
printf("Your Name:%s",pa->name);
printf("nYour Department:%s",pa->dept);
printf("n Your Roll no:%d",pa->roll_no);
printf("n***********************n");
++pa->roll_no;
printf("Your Name:%s",pa->name);
printf("nYour Department:%s",pa->dept);
printf("n Your Roll no:%d",pa->roll_no);
}
studinfo a,*pa;
pa=&a;
printf("What is your name? ");
scanf(" %[^n]",a.name);
printf("n Your department ");
scanf(" %[^n]",a.dept);
printf("n Your Roll number ");
scanf("%d",&a.roll_no);
printf("n***********************n");
printf("Your Name:%s",pa->name);
printf("nYour Department:%s",pa->dept);
printf("n Your Roll no:%d",pa->roll_no);
printf("n***********************n");
a.roll_no =update(a.roll_no);
printf("Your Name:%s",pa->name);
printf("nYour Department:%s",pa->dept);
printf("n Your Roll no:%d",pa->roll_no);
}
Unions
• Unions, like structures, contain members
whose individual data types may differ from
one another
• members within a union all share the same
storage area within the computer’s memorystorage area within the computer’s memory
• each member within a structure is assigned its
own unique storage area
Contd…
• unions are used to conserve memory
• are useful for applications involving multiple
members, where values need not be assigned
to all of the members at any one timeto all of the members at any one time
Contd…
union tag {
member 1 ;
member 2;
. . . . .
member m;
storage-class union tag {
member 1 ;
member 2;
. . . . .
member m;
member m;
}; member m;
} variable 1 , variable 2, . . .,
variable n;
#include<stdio.h>
typedef union{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a;
printf("a store %d bytes",sizeof(a));
}
#include<stdio.h>
typedef union{
char name[80];
char dept[30];
int roll_no;
}studinfo;
main()
{
studinfo a;studinfo a;
printf("What is your name? ");
scanf(" %[^n]",a.name);
printf("n Your department ");
scanf(" %[^n]",a.dept);
printf("n Your Roll number ");
scanf("%d",&a.roll_no);
printf("n***********************n");
printf("Your Name:%s",a.name);
printf("nYour Department:%s",a.dept);
printf("n Your Roll no:%d",a.roll_no);
}

More Related Content

Similar to STRUCTURES AND UNIONS IN C

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation Hemantha Kulathilake
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptxBoni Yeamin
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Smit Shah
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structureUtsav276
 
C programs Set 3
C programs Set 3C programs Set 3
C programs Set 3Koshy Geoji
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfHimanshuKansal22
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classesAlisha Korpal
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxArvindShukla81
 

Similar to STRUCTURES AND UNIONS IN C (20)

Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Structures
StructuresStructures
Structures
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptx
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Structures
StructuresStructures
Structures
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
C programs Set 3
C programs Set 3C programs Set 3
C programs Set 3
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
Arrays
ArraysArrays
Arrays
 

More from Kathmandu University (8)

Storage class
Storage classStorage class
Storage class
 
Pointers [compatibility mode]
Pointers [compatibility mode]Pointers [compatibility mode]
Pointers [compatibility mode]
 
L7 pointers
L7 pointersL7 pointers
L7 pointers
 
Array
ArrayArray
Array
 
Function
Function Function
Function
 
Looping
LoopingLooping
Looping
 
control statement
control statement control statement
control statement
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

STRUCTURES AND UNIONS IN C

  • 7. #include<stdio.h> typedef struct{ char name[80]; char dept[30]; int roll_no; } studinfo; Size of a Structure main() { studinfo a; printf("a store %d bytes",sizeof(a)); }
  • 8. struct date { char name[80]; int month; int day; int year; } struct date birthday[ ] = {“Amy”, 12, 30, 73, “Gail”, 5, 13, 66, “Marc”, 7, 15, 72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4, 12,69};
  • 9. #include<stdio.h> struct studinfo{ char name[80]; char dept[30]; int roll_no; }; main() { struct studinfo si; printf("Your name:");printf("Your name:"); scanf(“ %[^n]",si.name); printf("nYour department:"); scanf("%s",si.dept); printf("nYour roll number:"); scanf("%d",&si.roll_no); printf("*************************"); printf("nYour Name:%s",si.name); printf("nYour Department:%s",si.dept); printf("nYour Roll Number:%d",si.roll_no); }
  • 10. #include<stdio.h> struct date{ int month; int day; int year; }; struct studinfo{ char name[80]; char dept[30]; int roll_no; main( ) { struct studinfo si; printf("Your name:"); scanf(“ %[^n]",si.name); printf("nYour department:"); scanf("%s",si.dept); printf("nYour roll number:"); scanf("%d",&si.roll_no); printf("nDate of birth:(mm/dd/yy)"); scanf("%d%d%d",&si.birth.month,&si.birth.day,&si.birth.year); printf("*****************************"); printf("nYour Name:%s",si.name); printf("nYour Department:%s",si.dept); printf("nYour Roll Number:%d",si.roll_no); printf("nYour Birthday:%d-%d- int roll_no; struct date birth; }; printf("nYour Birthday:%d-%d- %d",si.birth.month,si.birth.day,si.birth.year); }
  • 11. #include<stdio.h> struct studinfo{ char name[80]; char dept[30]; int roll_no; }; main() { int n,i; struct studinfo si[100]; printf("*****************************n"); printf("******************************"); printf("n"); for(i=0;i<n;i++) { printf("nYour Name:%s",si[i].name); printf("nYour Department:%s",si[i].dept); printf("nYour Roll Number:%d",si[i].roll_no); printf("n************************************"); } } struct studinfo si[100]; printf("How many student information are you storing: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Your name:"); scanf(“ %[^n]",si[i].name); printf("Your department:"); scanf(“ %[^n]",si[i].dept); printf("Your roll number:"); scanf("%d",&si[i].roll_no); }
  • 12. #include<stdio.h> struct studinfo{ char name[80]; char dept[30]; int roll_no; }; main() { int n,i; struct studinfo si[100]; printf("*****************************n"); printf("******************************"); printf("n"); for(i=0;i<n;i++) { printf("nYour Name:%s",si[i].name); printf("nYour Department:%s",si[i].dept); printf("nYour Roll Number:%d",si[i].roll_no); printf("n************************************"); } } struct studinfo si[100]; printf("How many student information are you storing: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Your name:"); scanf(“ %[^n]",si[i].name); printf("Your department:"); scanf(“ %[^n]",si[i].dept); printf("Your roll number:"); scanf("%d",&si[i].roll_no); }
  • 13. #include<stdio.h> struct date{ int month; int day; int year; }; struct studinfo{ char name[80]; char dept[30]; main() { int n,i; struct studinfo si[100]; printf("How many student information are you storing: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Your name:"); scanf(“ %[^n]",si[i].name); printf("Your department:");char dept[30]; int roll_no; struct date birth; }; printf("Your department:"); fflush(stdin); scanf(“ %[^n]",si[i].dept); printf("Your roll number:"); scanf("%d",&si[i].roll_no); printf("nDate of birth:(mm/dd/yy)"); scanf("%d%d%d",&si[i].birth.month,&si[i].birth.d ay,&si[i].birth.year); }
  • 18. Defining date type: using typedef
  • 20. Parameter Passing in a function
  • 21.
  • 26.
  • 29.
  • 30. Contd… storage-class struct tag { member 1 ; member 2; . . . . . member m;member m; }variable 1, variable 2, . . ., variable n; it is possible to combine the declaration of the structure composition with that of the structure variables
  • 31. Contd… struct account { int acct_no; char acct_type; char name[80]; float balance ;float balance ; } oldcustomer, newcustomer; oldcustomer and newcustomer are structure variables of type account
  • 32. Contd… struct date { int month; int day; int year; }; struct account { int acct_no; char acct_type; char name[80]; float balance ; struct date lastpayment; };
  • 33. Contd… struct account customer = {12345, ‘R’, “John W. Smith”, 586.30, 5, 24, 90}; //structure name account //structure variable customer Members: acct_no = 12345; acct_type= ‘R’; name[80] = “John W. Smith”; balance = 586.30; month = 5; day = 24; year = 90
  • 34. Declaration: Structure Variable as an array struct date { int month; int day; int year; }; struct account { int acct-no; struct date { int month; int day; int year; } ; struct account { int acct_no; char acct-type;int acct-no; char acct-type; char name[80]; float balance; struct date lastpayment; } customer[ 100]; char acct-type; char name[80]; float balance ; struct date lastpayment; } ; struct account customer[100];
  • 35. struct date { char name[80]; int month; int day; int year; } struct date birthday[ ] = {“Amy”, 12, 30, 73, “Gail”, 5, 13, 66, “Marc”, 7, 15, 72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4,72, “Marla”, 11, 29, 70,”Megan”, 2, 4, 77,”Sharonw”,12, 29, 63, “Susan”, 4, 12,69};
  • 36. #include<stdio.h> struct studinfo{ char name[80]; char dept[30]; int roll_no; }; main() { struct studinfo si; printf("Your name:");printf("Your name:"); scanf(“ %[^n]",si.name); printf("nYour department:"); scanf("%s",si.dept); printf("nYour roll number:"); scanf("%d",&si.roll_no); printf("*************************"); printf("nYour Name:%s",si.name); printf("nYour Department:%s",si.dept); printf("nYour Roll Number:%d",si.roll_no); }
  • 37. #include<stdio.h> struct date{ int month; int day; int year; }; struct studinfo{ char name[80]; char dept[30]; int roll_no; main( ) { struct studinfo si; printf("Your name:"); scanf(“ %[^n]",si.name); printf("nYour department:"); scanf("%s",si.dept); printf("nYour roll number:"); scanf("%d",&si.roll_no); printf("nDate of birth:(mm/dd/yy)"); scanf("%d%d%d",&si.birth.month,&si.birth.day,&si.birth.year); printf("*****************************"); printf("nYour Name:%s",si.name); printf("nYour Department:%s",si.dept); printf("nYour Roll Number:%d",si.roll_no); printf("nYour Birthday:%d-%d- int roll_no; struct date birth; }; printf("nYour Birthday:%d-%d- %d",si.birth.month,si.birth.day,si.birth.year); }
  • 38. #include<stdio.h> struct studinfo{ char name[80]; char dept[30]; int roll_no; }; main() { int n,i; struct studinfo si[100]; printf("*****************************n"); printf("******************************"); printf("n"); for(i=0;i<n;i++) { printf("nYour Name:%s",si[i].name); printf("nYour Department:%s",si[i].dept); printf("nYour Roll Number:%d",si[i].roll_no); printf("n************************************"); } } struct studinfo si[100]; printf("How many student information are you storing: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Your name:"); scanf(“ %[^n]",si[i].name); printf("Your department:"); scanf(“ %[^n]",si[i].dept); printf("Your roll number:"); scanf("%d",&si[i].roll_no); }
  • 39. #include<stdio.h> struct date{ int month; int day; int year; }; struct studinfo{ char name[80]; char dept[30]; main() { int n,i; struct studinfo si[100]; printf("How many student information are you storing: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Your name:"); scanf(“ %[^n]",si[i].name); printf("Your department:");char dept[30]; int roll_no; struct date birth; }; printf("Your department:"); fflush(stdin); scanf(“ %[^n]",si[i].dept); printf("Your roll number:"); scanf("%d",&si[i].roll_no); printf("nDate of birth:(mm/dd/yy)"); scanf("%d%d%d",&si[i].birth.month,&si[i].birth.d ay,&si[i].birth.year); }
  • 40. printf("*****************************n"); printf("******************************"); printf("n"); for(i=0;i<n;i++){ printf("nYour Name:%s",si[i].name); printf("nYour Department:%s",si[i].dept); printf("nYour Roll Number:%d",si[i].roll_no); printf("nYour Birthday:%d-%d- %d",si[i].birth.month,si[i].birth.day,si[i].birth.year); printf("n************************************");printf("n************************************"); } }
  • 41. typedef • typedef feature allows users to define new data-types that are equivalent to existing data types • new data type is defined as typedef type new- type; typedef int age; //Example #include<stdio.h> main() { typedef int sk; sk a,b,sum; printf("Enter two number"); scanf("%d%d",&a,&b);typedef int age; age male, female; is equivalent to writing int male, female; scanf("%d%d",&a,&b); sum=a+b; printf("sum of %d and %d is %d",a,b,sum); }
  • 42. Contd… struct studinfo{ char name[80]; char dept[30]; int roll_no; }; struct studinfo a,b; typedef studinfo{ char name[80]; char dept[30]; int roll_no; } studinfo; studinfo a,*pa;
  • 43. #include<stdio.h> typedef struct{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a,*pa; pa=&a; printf("What is your name? "); scanf(" %[^n]",a.name); #include<stdio.h> typedef struct{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a,*pa; pa=&a; printf("What is your name? ");scanf(" %[^n]",a.name); printf("n Your department "); scanf(" %[^n]",a.dept); printf("n Your Roll number "); scanf("%d",&a.roll_no); printf("n***********************n"); printf("Your Name:%s",pa->name); printf("nYour Department:%s",pa- >dept); printf("n Your Roll no:%d",pa->roll_no); } printf("What is your name? "); scanf(" %[^n]",a.name); printf("n Your department "); scanf(" %[^n]",a.dept); printf("n Your Roll number "); scanf("%d",&a.roll_no); printf("n***********************n"); printf("Your Name:%s",(*pa).name); printf("nYour Department:%s",(*pa).dept); printf("n Your Roll no:%d",(*pa).roll_no); }
  • 44. #include<stdio.h> typedef struct{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a,*pa; pa=&a; printf("What is your name? "); scanf(" %[^n]",a.name); printf("n Your department "); scanf(" %[^n]",a.dept); #include<stdio.h> int update(int n) { n=n+1; return n; } typedef struct{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a,*pa;scanf(" %[^n]",a.dept); printf("n Your Roll number "); scanf("%d",&a.roll_no); printf("n***********************n"); printf("Your Name:%s",pa->name); printf("nYour Department:%s",pa->dept); printf("n Your Roll no:%d",pa->roll_no); printf("n***********************n"); ++pa->roll_no; printf("Your Name:%s",pa->name); printf("nYour Department:%s",pa->dept); printf("n Your Roll no:%d",pa->roll_no); } studinfo a,*pa; pa=&a; printf("What is your name? "); scanf(" %[^n]",a.name); printf("n Your department "); scanf(" %[^n]",a.dept); printf("n Your Roll number "); scanf("%d",&a.roll_no); printf("n***********************n"); printf("Your Name:%s",pa->name); printf("nYour Department:%s",pa->dept); printf("n Your Roll no:%d",pa->roll_no); printf("n***********************n"); a.roll_no =update(a.roll_no); printf("Your Name:%s",pa->name); printf("nYour Department:%s",pa->dept); printf("n Your Roll no:%d",pa->roll_no); }
  • 45. Unions • Unions, like structures, contain members whose individual data types may differ from one another • members within a union all share the same storage area within the computer’s memorystorage area within the computer’s memory • each member within a structure is assigned its own unique storage area
  • 46. Contd… • unions are used to conserve memory • are useful for applications involving multiple members, where values need not be assigned to all of the members at any one timeto all of the members at any one time
  • 47. Contd… union tag { member 1 ; member 2; . . . . . member m; storage-class union tag { member 1 ; member 2; . . . . . member m; member m; }; member m; } variable 1 , variable 2, . . ., variable n;
  • 48. #include<stdio.h> typedef union{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a; printf("a store %d bytes",sizeof(a)); }
  • 49. #include<stdio.h> typedef union{ char name[80]; char dept[30]; int roll_no; }studinfo; main() { studinfo a;studinfo a; printf("What is your name? "); scanf(" %[^n]",a.name); printf("n Your department "); scanf(" %[^n]",a.dept); printf("n Your Roll number "); scanf("%d",&a.roll_no); printf("n***********************n"); printf("Your Name:%s",a.name); printf("nYour Department:%s",a.dept); printf("n Your Roll no:%d",a.roll_no); }