SlideShare a Scribd company logo
C Data Types: Primary data types  Derived data types User-defined data types  Derived Types Function Type Structure Type Array Type Pointer Type Union Type Array  –  Collection of one or more related variables of similar  data type grouped under a single name Structure – Collection of one or more related variables of different  data types, grouped under a single name  In a Library, each book is an  object , and its  characteristics  like title, author, no of pages, price are grouped and represented by one  record . The characteristics are different types and grouped under a aggregate variable of different types. A  record  is group of  fields  and each field represents one characteristic.  In C, a record is implemented with a derived data type called  structure . The characteristics of record are called the  members  of the structure.
book bookid title author pages price STRUCTURE- BOOK struct  book  { int  book_id ; char title[50] ;  char author[40] ;  int pages ; float price ; }; Book-1 BookID: 1211 Title : C Primer Plus Author : Stephen Prata Pages : 984 Price : Rs. 585.00 Book-2 BookID: 1212 Title : The ANSI C Programming Author : Dennis Ritchie Pages : 214 Price : Rs. 125.00 Book-3 BookID: 1213 Title : C By Example Author :  Greg Perry Pages : 498 Price : Rs. 305.00 Structure tag integer book_id Array of 50 characters title Array of 40 characters author integer pages float price 2 bytes 50 bytes 40 bytes 2 bytes 4 bytes struct  < structure_tag_name >  { data type < member 1 > data type < member 2 > … .  ….  ….  …. data type < member N > } ; Memory occupied by a Structure variable
Initialization of structure  Initialization of structure variable while declaration : struct  student  s2 =  {  1001, “ K.Avinash ”, 87.25 } ; Initialization  of structure members individually :  s1. roll_no = 1111; strcpy ( s1. name , “ B. Kishore “ ) ; s1.percentage = 78.5 ;  Declaring a Structure Type struct student { int roll_no; char name[30]; float percentage; }; Declaring a Structure Variable  struct student s1,s2,s3; (or) struct student { int roll_no; char name[30]; float percentage; }s1,s2,s3; Reading values to members at runtime: struct student s3; printf(“\nEnter the roll no”); scanf(“%d”,&s3.roll_no); printf(“\nEnter the name”); scanf(“%s”,s3.name); printf(“\nEnter the percentage”); scanf(“%f”,&s3.percentage); membership operator
struct employee { int empid; char name[35]; int age; float salary; }; int main()  { struct employee emp1,emp2 ; struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ; emp1.empid=1211; strcpy(emp1.name, “K.Ravi”); emp1.age = 27; emp1.salary=30000.00; printf(“Enter the details of employee 2”); scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary); if(emp1.age > emp2.age)  printf( “ Employee1 is senior than Employee2\n” ); else  printf(“Employee1 is junior than Employee2\n”); printf(“Emp ID:%d\n  Name:%s\n  Age:%d\n  Salary:%f”,  emp1.empid,emp1.name,emp1.age,emp1.salary); } Implementing a Structure  Declaration of Structure Type  Declaration of Structure variables Declaration and initialization of Structure variable Initialization of Structure members individually  Reading values to members of Structure Accessing members of Structure
Nesting  of  structures struct date { int day ; int month ; int year ; } ; struct  person {  char  name[40]; int  age ; struct  date  b_day ;  }; int  main( )  { struct  person  p1; strcpy ( p1.name , “S. Ramesh “ ) ; p1. age =  32 ; p1.b_day.day  = 25 ; p1.b_day. month = 8 ; p1.b_day. year = 1978 ; } Arrays  And structures struct student { int sub[3] ; int total ; } ; int  main( )  { struct student s[3]; int i,j; for(i=0;i<3;i++) { printf(“\n\nEnter student %d marks:”,i+1); for(j=0;j<3;j++) { scanf(“%d”,&s[i].sub[j]); } } for(i=0;i<3;i++) { s[i].total =0; for(j=0;j<3;j++) { s[i].total +=s[i].sub[j]; } printf(“\nTotal marks of student %d is: %d”, i+1,s[i].total ); }  } OUTPUT: Enter student 1 marks: 60 60 60 Enter student 2 marks: 70 70 70 Enter student 3 marks: 90 90 90 Total marks of  student 1 is: 180 Total marks of  student 2 is: 240 Total marks of  student 3 is: 270 Outer Structure  Inner Structure  Accessing Inner Structure members
struct  fraction  { int numerator ; int denominator ; }; void  show ( struct fraction f  ) { printf ( “ %d / %d “, f.numerator,  f.denominator ) ; } int  main ( )  { struct fraction f1  =  {  7, 12 } ; show  ( f1 )  ; } OUTPUT: 7 / 12 structures and functions Self referential structures struct student_node  { int roll_no ; char  name [25] ; struct student_node  *next ; } ; int  main( )  { struct  student_node  s1 ; struct  student_node  s2 = { 1111, “B.Mahesh”, NULL } ; s1. roll_no  = 1234 ;  strcpy ( s1.name , “P.Kiran “ ) ;  s1. next  =  & s2 ; printf ( “ %s “, s1. name  ) ; printf ( “ %s “ , s1.next - > name  ) ;  } A self referential structure is one that includes at least one member which is a pointer to the same structure type. With self referential structures, we can create very useful data structures such as linked -lists, trees and graphs . s2 node is linked to s1 node Prints P.Kiran Prints B.Mahesh
Pointer to a structure  Accessing  structure members through pointer : i) Using  .  ( dot ) operator : ( *ptr ) . prodid  =  111 ; strcpy ( ( *ptr ) . Name, “Pen”) ; ii) Using  - > ( arrow ) operator : ptr - > prodid =  111 ; strcpy( ptr - > name , “Pencil”) ; struct product  { int prodid; char name[20]; }; int main() { struct product inventory[3]; struct product  *ptr; printf(“Read Product Details : \n&quot;); for(ptr = inventory;ptr<inventory +3;ptr++) { scanf(&quot;%d %s&quot;, &ptr->prodid, ptr->name); } printf(&quot;\noutput\n&quot;); for(ptr=inventory;ptr<inventory+3;ptr++) { printf(&quot;\n\nProduct ID :%5d&quot;,ptr->prodid); printf(&quot;\nName :  %s&quot;,ptr->name);  } } Read Product Details : 111 Pen 112 Pencil 113 Book Print Product Details : Product ID : 111 Name : Pen Product ID : 112 Name : Pencil Product ID : 113 Name : Book
A  union is a structure all of whose members share the same memory  Union  is a variable, which is similar to the  structure  and contains number of members like structure. In the structure each member has its own memory location whereas, members of union share the same memory. The amount of storage allocated to a union is sufficient to hold its largest member. struct  student  { int  rollno; float  avg ; char  grade ; }; union  pupil {  int  rollno; float  avg ; char  grade; } ; int main()  { struct  student  s1 ; union pupil  p1; printf ( “ %d bytes “,  sizeof ( struct student ) ) ; printf ( “ %d bytes “, sizeof ( union pupil ) ) ;  } Output : 7 bytes  4 bytes Memory allotted to structure student  Address  5000  5001  5002  5003  5004  5005  5006   rollno avg grade Total memory occupied  :  7  bytes Memory allotted to union pupil  rollno avg grade Total memory occupied  :  4  bytes Address  5000  5001  5002  5003

More Related Content

PPTX
Basics of Java
PPT
C++ Arrays
PDF
sparse matrix in data structure
PPT
Stacks and queue
PPT
Arrays in c
PPTX
17. Java data structures trees representation and traversal
PPTX
Variadic functions
PDF
Chapter Functions for grade 12 computer Science
Basics of Java
C++ Arrays
sparse matrix in data structure
Stacks and queue
Arrays in c
17. Java data structures trees representation and traversal
Variadic functions
Chapter Functions for grade 12 computer Science

What's hot (20)

PDF
Java String
PPT
Python Pandas
PPTX
Structure in C language
PPT
Multidimensional array in C
PDF
Pointers and Structures
PPT
Linked lists
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
PDF
Functions and modules in python
PPTX
PPT
Stacks overview with its applications
PPTX
Dynamic Memory Allocation(DMA)
PPTX
Linear data structure concepts
PPT
Pointers - DataStructures
PPTX
Data structure Stack
PPT
structure and union
PPTX
Stack and Queue
PPT
Structure in c
PDF
Constructor and Destructor
PPTX
PROLOG: Database Manipulation In Prolog
PPTX
Dynamic memory allocation in c
Java String
Python Pandas
Structure in C language
Multidimensional array in C
Pointers and Structures
Linked lists
Computer Science-Data Structures :Abstract DataType (ADT)
Functions and modules in python
Stacks overview with its applications
Dynamic Memory Allocation(DMA)
Linear data structure concepts
Pointers - DataStructures
Data structure Stack
structure and union
Stack and Queue
Structure in c
Constructor and Destructor
PROLOG: Database Manipulation In Prolog
Dynamic memory allocation in c
Ad

Viewers also liked (20)

DOCX
Diccionario de ingles español2 (reparado)
PPTX
2. museo municipal de madrid
PPTX
Neuromarketing POR CHRISTIAM MENDEZ
PDF
Mobile AirPlan 1-pager
 
PDF
Ducray Champú Sensinol Cabello Sensible
DOC
Eobd
PDF
How Kraft Drives Email Engagement Using Native Advertising & Personalization
PPT
Els Navegadors
DOCX
Informe del hogar la esperanza
PDF
Arquímedes 1º
PDF
Revista mandala literaria no.19
PDF
Guida Ecoidea 2 - La riduzione dei rifiuti all'acquisto
PDF
Programa Segundo curso Diplomatura en Cinematografía 2016/2017
PDF
Sookman lsuc copyright_year_in_review_2013_final
PPT
Mobile learning for gender equality
DOCX
Historia del calor
DOCX
El talento, los trastornos emocionales y otros
ODT
Bethabara . il battesimo di Gesù.
PDF
Firebird 1.5-quick start-spanish
PDF
Sådan laver du vellykket web-tv
Diccionario de ingles español2 (reparado)
2. museo municipal de madrid
Neuromarketing POR CHRISTIAM MENDEZ
Mobile AirPlan 1-pager
 
Ducray Champú Sensinol Cabello Sensible
Eobd
How Kraft Drives Email Engagement Using Native Advertising & Personalization
Els Navegadors
Informe del hogar la esperanza
Arquímedes 1º
Revista mandala literaria no.19
Guida Ecoidea 2 - La riduzione dei rifiuti all'acquisto
Programa Segundo curso Diplomatura en Cinematografía 2016/2017
Sookman lsuc copyright_year_in_review_2013_final
Mobile learning for gender equality
Historia del calor
El talento, los trastornos emocionales y otros
Bethabara . il battesimo di Gesù.
Firebird 1.5-quick start-spanish
Sådan laver du vellykket web-tv
Ad

Similar to Unit4 C (20)

PPT
Unit4
PPT
C Language Unit-4
PPTX
detail structure presentation of problem solving
PPTX
Intoduction to structure
PDF
Principals of Programming in CModule -5.pdf
PPTX
Structures
PPTX
data structure and c programing concepts
PPTX
CA2_CYS101_31184422012_Arvind-Shukla.pptx
PPTX
U5 SPC.pptx
PPT
structures.ppt
PPT
Introduction to structures in c lang.ppt
PPTX
Structure & union
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPT
Presentation on structure,functions and classes
PDF
VIT351 Software Development VI Unit4
DOC
Unit 5 (1)
PPTX
PPT
structure.ppt
Unit4
C Language Unit-4
detail structure presentation of problem solving
Intoduction to structure
Principals of Programming in CModule -5.pdf
Structures
data structure and c programing concepts
CA2_CYS101_31184422012_Arvind-Shukla.pptx
U5 SPC.pptx
structures.ppt
Introduction to structures in c lang.ppt
Structure & union
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Programming for problem solving-II(UNIT-2).pptx
Presentation on structure,functions and classes
VIT351 Software Development VI Unit4
Unit 5 (1)
structure.ppt

More from arnold 7490 (20)

PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
Unit 8 Java
PPT
Unit 6 Java
PPT
Unit 5 Java
PPT
Unit 4 Java
PPT
Unit 3 Java
PPT
Unit 2 Java
PPT
Unit 1 Java
Unit 8 Java
Unit 6 Java
Unit 5 Java
Unit 4 Java
Unit 3 Java
Unit 2 Java
Unit 1 Java

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Big Data Technologies - Introduction.pptx
A Presentation on Artificial Intelligence
Review of recent advances in non-invasive hemoglobin estimation
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation_ Review paper, used for researhc scholars
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Unit4 C

  • 1. C Data Types: Primary data types Derived data types User-defined data types Derived Types Function Type Structure Type Array Type Pointer Type Union Type Array – Collection of one or more related variables of similar data type grouped under a single name Structure – Collection of one or more related variables of different data types, grouped under a single name In a Library, each book is an object , and its characteristics like title, author, no of pages, price are grouped and represented by one record . The characteristics are different types and grouped under a aggregate variable of different types. A record is group of fields and each field represents one characteristic. In C, a record is implemented with a derived data type called structure . The characteristics of record are called the members of the structure.
  • 2. book bookid title author pages price STRUCTURE- BOOK struct book { int book_id ; char title[50] ; char author[40] ; int pages ; float price ; }; Book-1 BookID: 1211 Title : C Primer Plus Author : Stephen Prata Pages : 984 Price : Rs. 585.00 Book-2 BookID: 1212 Title : The ANSI C Programming Author : Dennis Ritchie Pages : 214 Price : Rs. 125.00 Book-3 BookID: 1213 Title : C By Example Author : Greg Perry Pages : 498 Price : Rs. 305.00 Structure tag integer book_id Array of 50 characters title Array of 40 characters author integer pages float price 2 bytes 50 bytes 40 bytes 2 bytes 4 bytes struct < structure_tag_name > { data type < member 1 > data type < member 2 > … . …. …. …. data type < member N > } ; Memory occupied by a Structure variable
  • 3. Initialization of structure Initialization of structure variable while declaration : struct student s2 = { 1001, “ K.Avinash ”, 87.25 } ; Initialization of structure members individually : s1. roll_no = 1111; strcpy ( s1. name , “ B. Kishore “ ) ; s1.percentage = 78.5 ; Declaring a Structure Type struct student { int roll_no; char name[30]; float percentage; }; Declaring a Structure Variable struct student s1,s2,s3; (or) struct student { int roll_no; char name[30]; float percentage; }s1,s2,s3; Reading values to members at runtime: struct student s3; printf(“\nEnter the roll no”); scanf(“%d”,&s3.roll_no); printf(“\nEnter the name”); scanf(“%s”,s3.name); printf(“\nEnter the percentage”); scanf(“%f”,&s3.percentage); membership operator
  • 4. struct employee { int empid; char name[35]; int age; float salary; }; int main() { struct employee emp1,emp2 ; struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ; emp1.empid=1211; strcpy(emp1.name, “K.Ravi”); emp1.age = 27; emp1.salary=30000.00; printf(“Enter the details of employee 2”); scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary); if(emp1.age > emp2.age) printf( “ Employee1 is senior than Employee2\n” ); else printf(“Employee1 is junior than Employee2\n”); printf(“Emp ID:%d\n Name:%s\n Age:%d\n Salary:%f”, emp1.empid,emp1.name,emp1.age,emp1.salary); } Implementing a Structure Declaration of Structure Type Declaration of Structure variables Declaration and initialization of Structure variable Initialization of Structure members individually Reading values to members of Structure Accessing members of Structure
  • 5. Nesting of structures struct date { int day ; int month ; int year ; } ; struct person { char name[40]; int age ; struct date b_day ; }; int main( ) { struct person p1; strcpy ( p1.name , “S. Ramesh “ ) ; p1. age = 32 ; p1.b_day.day = 25 ; p1.b_day. month = 8 ; p1.b_day. year = 1978 ; } Arrays And structures struct student { int sub[3] ; int total ; } ; int main( ) { struct student s[3]; int i,j; for(i=0;i<3;i++) { printf(“\n\nEnter student %d marks:”,i+1); for(j=0;j<3;j++) { scanf(“%d”,&s[i].sub[j]); } } for(i=0;i<3;i++) { s[i].total =0; for(j=0;j<3;j++) { s[i].total +=s[i].sub[j]; } printf(“\nTotal marks of student %d is: %d”, i+1,s[i].total ); } } OUTPUT: Enter student 1 marks: 60 60 60 Enter student 2 marks: 70 70 70 Enter student 3 marks: 90 90 90 Total marks of student 1 is: 180 Total marks of student 2 is: 240 Total marks of student 3 is: 270 Outer Structure Inner Structure Accessing Inner Structure members
  • 6. struct fraction { int numerator ; int denominator ; }; void show ( struct fraction f ) { printf ( “ %d / %d “, f.numerator, f.denominator ) ; } int main ( ) { struct fraction f1 = { 7, 12 } ; show ( f1 ) ; } OUTPUT: 7 / 12 structures and functions Self referential structures struct student_node { int roll_no ; char name [25] ; struct student_node *next ; } ; int main( ) { struct student_node s1 ; struct student_node s2 = { 1111, “B.Mahesh”, NULL } ; s1. roll_no = 1234 ; strcpy ( s1.name , “P.Kiran “ ) ; s1. next = & s2 ; printf ( “ %s “, s1. name ) ; printf ( “ %s “ , s1.next - > name ) ; } A self referential structure is one that includes at least one member which is a pointer to the same structure type. With self referential structures, we can create very useful data structures such as linked -lists, trees and graphs . s2 node is linked to s1 node Prints P.Kiran Prints B.Mahesh
  • 7. Pointer to a structure Accessing structure members through pointer : i) Using . ( dot ) operator : ( *ptr ) . prodid = 111 ; strcpy ( ( *ptr ) . Name, “Pen”) ; ii) Using - > ( arrow ) operator : ptr - > prodid = 111 ; strcpy( ptr - > name , “Pencil”) ; struct product { int prodid; char name[20]; }; int main() { struct product inventory[3]; struct product *ptr; printf(“Read Product Details : \n&quot;); for(ptr = inventory;ptr<inventory +3;ptr++) { scanf(&quot;%d %s&quot;, &ptr->prodid, ptr->name); } printf(&quot;\noutput\n&quot;); for(ptr=inventory;ptr<inventory+3;ptr++) { printf(&quot;\n\nProduct ID :%5d&quot;,ptr->prodid); printf(&quot;\nName : %s&quot;,ptr->name); } } Read Product Details : 111 Pen 112 Pencil 113 Book Print Product Details : Product ID : 111 Name : Pen Product ID : 112 Name : Pencil Product ID : 113 Name : Book
  • 8. A union is a structure all of whose members share the same memory Union is a variable, which is similar to the structure and contains number of members like structure. In the structure each member has its own memory location whereas, members of union share the same memory. The amount of storage allocated to a union is sufficient to hold its largest member. struct student { int rollno; float avg ; char grade ; }; union pupil { int rollno; float avg ; char grade; } ; int main() { struct student s1 ; union pupil p1; printf ( “ %d bytes “, sizeof ( struct student ) ) ; printf ( “ %d bytes “, sizeof ( union pupil ) ) ; } Output : 7 bytes 4 bytes Memory allotted to structure student Address 5000 5001 5002 5003 5004 5005 5006 rollno avg grade Total memory occupied : 7 bytes Memory allotted to union pupil rollno avg grade Total memory occupied : 4 bytes Address 5000 5001 5002 5003