SlideShare a Scribd company logo
1 of 13
What is structure?
In C programming, a struct (or
structure) is a collection of variables
(can be of different types) under a
single name.
How to define structures?
Before you can create structure
variables, you need to define its
data type. To define a struct, the
struct keyword is used.
Syntax of struct :
struct structureName
{
dataType member1;
dataType member2;
...
};
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Here's how we create structure variables:
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
Another way of creating a struct variable is:
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1, person2,
and an array variable p having 20 elements of
type struct Person are created.
Access members of a structure
There are two types of operators used for accessing
members of a structure.
1. . - Member operator
2. - > - Structure pointer operator
Suppose, you want to access the salary of person2.
Here's how you can do it.
person2.salary
// Program to add two distances (feet-inch)
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;
int main()
{
printf("1st distancen");
printf("Enter feet: ");
scanf("%d", &dist1.feet);
printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distancen");
printf("Enter feet: ");
scanf("%d", &dist2.feet);
Example: Add two distances
printf("Enter inch: ");
scanf("%f", &dist2.inch);
// adding feet
sum.feet = dist1.feet + dist2.feet;
// adding inches
sum.inch = dist1.inch + dist2.inch;
// changing to feet if inch is greater than 12
while (sum.inch >= 12)
{
++sum.feet;
sum.inch = sum.inch - 12;
}
printf("Sum of distances = %d'-%.1f"", sum.feet, sum.inch);
return 0;
}
Output :
1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"
#include <stdio.h>
typedef struct book{
int id;
char name;
} book;
int main ()
{
book book1;
book book2;
book book3;
book1.id = 1;
book1.name = 'A';
book2.id = 2;
book2.name = 'B';
book3.id = 3;
book3.name = 'C';
Now we write the research program, once three examples of the structure will be created.
printf( "Book ID : %dn", book1.id);
printf( "Book Name : %cn n", book1.name);
printf( "Book ID : %dn", book2.id);
printf( "Book Name : %cn n", book2.name);
printf( "Book ID : %dn", book3.id);
printf( "Book Name : %cn", book3.name);
return 0;
}
Book ID : 1
Book Name : A
Book ID : 2
Book Name : B
Book ID : 3
Book Name : C
Output:
Thank you.

More Related Content

What's hot (18)

week-21x
week-21xweek-21x
week-21x
 
Cara membuat tulisan mengikuti kursor di blog
Cara membuat tulisan mengikuti kursor di blogCara membuat tulisan mengikuti kursor di blog
Cara membuat tulisan mengikuti kursor di blog
 
今王必欲致士 先従隗始 - little debugging principles
今王必欲致士 先従隗始 - little debugging principles今王必欲致士 先従隗始 - little debugging principles
今王必欲致士 先従隗始 - little debugging principles
 
Circular queue
Circular queueCircular queue
Circular queue
 
Empolyee deatils in java
Empolyee deatils in javaEmpolyee deatils in java
Empolyee deatils in java
 
Programming Language: Ruby
Programming Language: RubyProgramming Language: Ruby
Programming Language: Ruby
 
Oopsprc1e
Oopsprc1eOopsprc1e
Oopsprc1e
 
Array matrix example programs - C language
Array matrix example programs - C languageArray matrix example programs - C language
Array matrix example programs - C language
 
Computer lab (programs)
Computer lab (programs)Computer lab (programs)
Computer lab (programs)
 
Ooprc4 b
Ooprc4 bOoprc4 b
Ooprc4 b
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]
 
10 2 배열 응용
10 2 배열 응용10 2 배열 응용
10 2 배열 응용
 
Cpp
Cpp Cpp
Cpp
 
Vcs29
Vcs29Vcs29
Vcs29
 
Snow
SnowSnow
Snow
 
言語の設計判断
言語の設計判断言語の設計判断
言語の設計判断
 
constants
constantsconstants
constants
 
References Are 'Nice' Pointers
References Are 'Nice' PointersReferences Are 'Nice' Pointers
References Are 'Nice' Pointers
 

Similar to Presentation for structure in c

Similar to Presentation for structure in c (20)

STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
Lab 13
Lab 13Lab 13
Lab 13
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
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
 
include ltstdiohgt include ltstringhgt define M.pdf
include ltstdiohgt include ltstringhgt define M.pdfinclude ltstdiohgt include ltstringhgt define M.pdf
include ltstdiohgt include ltstringhgt define M.pdf
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Tu1
Tu1Tu1
Tu1
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Ch6 pointers (latest)
Ch6 pointers (latest)Ch6 pointers (latest)
Ch6 pointers (latest)
 
02.adt
02.adt02.adt
02.adt
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 

Recently uploaded

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 

Recently uploaded (20)

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 

Presentation for structure in c

  • 1. What is structure? In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name.
  • 2. How to define structures? Before you can create structure variables, you need to define its data type. To define a struct, the struct keyword is used.
  • 3. Syntax of struct : struct structureName { dataType member1; dataType member2; ... }; Here is an example: struct Person { char name[50]; int citNo; float salary; };
  • 4. Here's how we create structure variables: struct Person { char name[50]; int citNo; float salary; }; int main() { struct Person person1, person2, p[20]; return 0; }
  • 5. Another way of creating a struct variable is: struct Person { char name[50]; int citNo; float salary; } person1, person2, p[20]; In both cases, two variables person1, person2, and an array variable p having 20 elements of type struct Person are created.
  • 6. Access members of a structure There are two types of operators used for accessing members of a structure. 1. . - Member operator 2. - > - Structure pointer operator Suppose, you want to access the salary of person2. Here's how you can do it. person2.salary
  • 7. // Program to add two distances (feet-inch) #include <stdio.h> struct Distance { int feet; float inch; } dist1, dist2, sum; int main() { printf("1st distancen"); printf("Enter feet: "); scanf("%d", &dist1.feet); printf("Enter inch: "); scanf("%f", &dist1.inch); printf("2nd distancen"); printf("Enter feet: "); scanf("%d", &dist2.feet); Example: Add two distances
  • 8. printf("Enter inch: "); scanf("%f", &dist2.inch); // adding feet sum.feet = dist1.feet + dist2.feet; // adding inches sum.inch = dist1.inch + dist2.inch; // changing to feet if inch is greater than 12 while (sum.inch >= 12) { ++sum.feet; sum.inch = sum.inch - 12; } printf("Sum of distances = %d'-%.1f"", sum.feet, sum.inch); return 0; }
  • 9. Output : 1st distance Enter feet: 12 Enter inch: 7.9 2nd distance Enter feet: 2 Enter inch: 9.8 Sum of distances = 15'-5.7"
  • 10. #include <stdio.h> typedef struct book{ int id; char name; } book; int main () { book book1; book book2; book book3; book1.id = 1; book1.name = 'A'; book2.id = 2; book2.name = 'B'; book3.id = 3; book3.name = 'C'; Now we write the research program, once three examples of the structure will be created.
  • 11. printf( "Book ID : %dn", book1.id); printf( "Book Name : %cn n", book1.name); printf( "Book ID : %dn", book2.id); printf( "Book Name : %cn n", book2.name); printf( "Book ID : %dn", book3.id); printf( "Book Name : %cn", book3.name); return 0; }
  • 12. Book ID : 1 Book Name : A Book ID : 2 Book Name : B Book ID : 3 Book Name : C Output: