SlideShare ist ein Scribd-Unternehmen logo
1 von 10
C Data Types:
Primary data types                       Derived
Derived data types                        Types
User-defined data types



    Function                                Pointer            Structure
                     Array Type                                                  Union Type
      Type                                   Type                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-1                      Book-2                               Book-3
BookID: 1211                BookID: 1212                         BookID: 1213
Title : C Primer Plus       Title : The ANSI C Programming       Title : C By Example
Author : Stephen Prata      Author : Dennis Ritchie              Author : Greg Perry
Pages : 984                 Pages : 214                          Pages : 498
Price : Rs. 585.00          Price : Rs. 125.00                   Price : Rs. 305.00

   book                        book_id        integer        2 bytes
                  bookid
                                 title        Array of 50 characters                    50 bytes
                    title
                                 author       Array of 40 characters            40 bytes
                  author
                                 pages        integer         2 bytes
                  pages
                   price         price        float                      4 bytes

                                         Memory occupied by a Structure variable

               STRUCTURE- BOOK                        struct < structure_tag_name >
struct book {                                         {
                            Structure tag                   data type < member 1 >
      int book_id ;
      char title[50] ;                                      data type < member 2 >
                                                               …. …. …. ….
      char author[40] ;
                                                            data type < member N >
      int pages ;                                     };
      float price ;
};
Declaring a Structure Type                   Declaring a Structure Variable

 struct student                               struct student s1,s2,s3;
 {                                                      (or)
           int roll_no;                       struct student
           char name[30];                     {
           float percentage;                            int roll_no;
 };                                                     char name[30];
                                                        float percentage;
                                              }s1,s2,s3;

      Initialization of structure                       Reading values to members at
                                                        runtime:
Initialization of structure variable while
declaration :                                           struct student s3;
   struct student s2 = { 1001, “ K.Avinash ”,           printf(“nEnter the roll no”);
                  87.25 } ;                             scanf(“%d”,&s3.roll_no);
Initialization of structure members individually :      printf(“nEnter the name”);
   s1. roll_no = 1111;                                  scanf(“%s”,s3.name);
   strcpy ( s1. name , “ B. Kishore “ ) ;               printf(“nEnter the percentage”);
   s1.percentage = 78.5 ;                               scanf(“%f”,&s3.percentage);

                      membership operator
Implementing a Structure
struct employee {
     int empid;
     char name[35];
     int age;                   Declaration of Structure Type
     float salary;
};
                                          Declaration of Structure variables
int main() {
    struct employee emp1,emp2 ;      Declaration and initialization of Structure variable

   struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ;
   emp1.empid=1211;
   strcpy(emp1.name, “K.Ravi”);
   emp1.age = 27;                         Initialization of Structure members individually
   emp1.salary=30000.00;
   printf(“Enter the details of employee 2”);      Reading values to members of Structure
   scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary);
   if(emp1.age > emp2.age)
        printf( “ Employee1 is senior than Employee2n” );
   else
        printf(“Employee1 is junior than Employee2n”);
                                                             Accessing members of Structure
  printf(“Emp ID:%dn Name:%sn Age:%dn Salary:%f”,
emp1.empid,emp1.name,emp1.age,emp1.salary);
}
Arrays And structures                               Nesting of structures
struct student                                        struct date {
{                                                         int day ;
   int sub[3] ;                                           int month ;
                                                                             Outer Structure
   int total ;                                            int year ;
};                                                    };
                                                      struct person {
                                                          char name[40];
int main( ) {                                             int age ;
  struct student s[3];                                    struct date b_day ;
  int i,j;                                            };
   for(i=0;i<3;i++) {                                 int main( ) {             Inner Structure
      printf(“nnEnter student %d marks:”,i+1);         struct person p1;
      for(j=0;j<3;j++) {                                 strcpy ( p1.name , “S. Ramesh “ ) ;
           scanf(“%d”,&s[i].sub[j]);                     p1. age = 32 ;
       }                                                 p1.b_day.day = 25 ;       Accessing Inner
    }                                                    p1.b_day. month = 8 ; Structure members
    for(i=0;i<3;i++) {                                   p1.b_day. year = 1978 ;
         s[i].total =0;                               }
         for(j=0;j<3;j++) {
               s[i].total +=s[i].sub[j];              OUTPUT:
       }                                              Enter student 1 marks: 60 60 60
       printf(“nTotal marks of student %d is: %d”,   Enter student 2 marks: 70 70 70
                          i+1,s[i].total );           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
structures and functions                           Self referential structures
struct fraction {                      struct student_node {
    int numerator ;                        int roll_no ;
    int denominator ;                      char name [25] ;
};                                         struct student_node *next ;
                                       };
void show ( struct fraction f )        int main( )
{                                       {
  printf ( “ %d / %d “, f.numerator,      struct student_node s1 ;
                                          struct student_node s2 = { 1111, “B.Mahesh”, NULL } ;
                f.denominator ) ;         s1. roll_no = 1234 ;
}                                         strcpy ( s1.name , “P.Kiran “ ) ;
int main ( ) {                             s1. next = & s2 ;       s2 node is linked to s1 node
    struct fraction f1 = { 7, 12 } ;
    show ( f1 ) ;                          printf ( “ %s “, s1. name ) ;
}                                                                            Prints P.Kiran
                                           printf ( “ %s “ , s1.next - > name ) ;   Prints B.Mahesh
OUTPUT:                                }
           7 / 12



         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.
Pointer to a structure

struct product                                     Accessing structure members through
{                                                  pointer :
   int prodid;
   char name[20];                                  i) Using . ( dot ) operator :
};                                                      ( *ptr ) . prodid = 111 ;
                                                        strcpy ( ( *ptr ) . Name, “Pen”) ;
int main()
{                                                  ii) Using - > ( arrow ) operator :
   struct product inventory[3];                          ptr - > prodid = 111 ;
   struct product *ptr;                                  strcpy( ptr - > name , “Pencil”) ;
   printf(“Read Product Details : n");
   for(ptr = inventory;ptr<inventory +3;ptr++) {
     scanf("%d %s", &ptr->prodid, ptr->name);      Read Product Details :
   }
                                                   111 Pen
   printf("noutputn");
                                                   112 Pencil
   for(ptr=inventory;ptr<inventory+3;ptr++)        113 Book
   {
     printf("nnProduct ID :%5d",ptr->prodid);    Print Product Details :
     printf("nName : %s",ptr->name);
   }                                               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;                              Memory allotted to structure student
    float avg ;
    char grade ;                     Address 5000     5001     5002    5003    5004 5005  5006
};
union pupil {
    int rollno;
    float avg ;                                  rollno                    avg           grade
    char grade;                                     Total memory occupied : 7 bytes
};
int main() {
    struct student s1 ;                           Memory allotted to union pupil
    union pupil p1;
    printf ( “ %d bytes “,            Address 5000        5001    5002    5003
       sizeof ( struct student ) ) ;
     printf ( “ %d bytes “,
        sizeof ( union pupil ) ) ;
}                                                  rollno
Output :                                                     avg
   7 bytes 4 bytes                             grade
                                              Total memory occupied : 4 bytes
www.jntuworld.com
• For More Materials, Text Books, Previous
  Papers & Mobile updates of B.TECH,
  B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU-
  KAKINADA & JNTU-ANANTAPUR visit
  www.jntuworld.com
www.jntuworld.com
• For More Materials, Text Books, Previous
  Papers & Mobile updates of B.TECH,
  B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU-
  KAKINADA & JNTU-ANANTAPUR visit
  www.jntuworld.com

Weitere ähnliche Inhalte

Was ist angesagt?

PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
Fitz Agard
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
MongoSF
 
Linear algebra review
Linear algebra reviewLinear algebra review
Linear algebra review
vevin1986
 

Was ist angesagt? (12)

PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
 
Jazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with EclipseJazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with Eclipse
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Linear algebra review
Linear algebra reviewLinear algebra review
Linear algebra review
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Php Map Script Class Reference
Php Map Script Class ReferencePhp Map Script Class Reference
Php Map Script Class Reference
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 

Andere mochten auch

Presentation Influencing Factors
Presentation Influencing FactorsPresentation Influencing Factors
Presentation Influencing Factors
taco_dols
 
Inverse Matrix & Determinants
Inverse Matrix & DeterminantsInverse Matrix & Determinants
Inverse Matrix & Determinants
itutor
 
EXPORT PROCEDURE & DOCUMENTATION
EXPORT PROCEDURE & DOCUMENTATIONEXPORT PROCEDURE & DOCUMENTATION
EXPORT PROCEDURE & DOCUMENTATION
vikas chauhan
 

Andere mochten auch (20)

Unit i
Unit iUnit i
Unit i
 
Ses 2 matrix opt
Ses 2 matrix optSes 2 matrix opt
Ses 2 matrix opt
 
Pc 8.4 notes
Pc 8.4 notesPc 8.4 notes
Pc 8.4 notes
 
Presentation Influencing Factors
Presentation Influencing FactorsPresentation Influencing Factors
Presentation Influencing Factors
 
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
 
Solving 2x2 systems using inverse matrix (December 12, 2013)
Solving 2x2 systems using inverse matrix (December 12, 2013)Solving 2x2 systems using inverse matrix (December 12, 2013)
Solving 2x2 systems using inverse matrix (December 12, 2013)
 
Attitude
AttitudeAttitude
Attitude
 
Effective communication skills
Effective communication skillsEffective communication skills
Effective communication skills
 
Determinants
DeterminantsDeterminants
Determinants
 
Application of derivatives
Application of derivatives Application of derivatives
Application of derivatives
 
Lecture 5 inverse of matrices - section 2-2 and 2-3
Lecture 5   inverse of matrices - section 2-2 and 2-3Lecture 5   inverse of matrices - section 2-2 and 2-3
Lecture 5 inverse of matrices - section 2-2 and 2-3
 
Determinants
DeterminantsDeterminants
Determinants
 
MATRICES
MATRICESMATRICES
MATRICES
 
Communication system
Communication systemCommunication system
Communication system
 
Presentation on inverse matrix
Presentation on inverse matrixPresentation on inverse matrix
Presentation on inverse matrix
 
Inverse Matrix & Determinants
Inverse Matrix & DeterminantsInverse Matrix & Determinants
Inverse Matrix & Determinants
 
Matrices And Application Of Matrices
Matrices And Application Of MatricesMatrices And Application Of Matrices
Matrices And Application Of Matrices
 
Various approaches to management
Various approaches to managementVarious approaches to management
Various approaches to management
 
EXPORT PROCEDURE & DOCUMENTATION
EXPORT PROCEDURE & DOCUMENTATIONEXPORT PROCEDURE & DOCUMENTATION
EXPORT PROCEDURE & DOCUMENTATION
 
EXPORT IMPORT
EXPORT IMPORTEXPORT IMPORT
EXPORT IMPORT
 

Ähnlich wie Unit4

Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
babuk110
 

Ähnlich wie Unit4 (20)

Unit4 C
Unit4 C Unit4 C
Unit4 C
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
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...
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structures
StructuresStructures
Structures
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdf
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
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
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
structure
structurestructure
structure
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 

Mehr von mrecedu

Brochure final
Brochure finalBrochure final
Brochure final
mrecedu
 
Filters unit iii
Filters unit iiiFilters unit iii
Filters unit iii
mrecedu
 
Attenuator unit iv
Attenuator unit ivAttenuator unit iv
Attenuator unit iv
mrecedu
 
Two port networks unit ii
Two port networks unit iiTwo port networks unit ii
Two port networks unit ii
mrecedu
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
mrecedu
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
mrecedu
 
Unit2 jwfiles
Unit2 jwfilesUnit2 jwfiles
Unit2 jwfiles
mrecedu
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
mrecedu
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfiles
mrecedu
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworld
mrecedu
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworld
mrecedu
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworld
mrecedu
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworld
mrecedu
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworld
mrecedu
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworld
mrecedu
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworld
mrecedu
 
M1 unit vii-jntuworld
M1 unit vii-jntuworldM1 unit vii-jntuworld
M1 unit vii-jntuworld
mrecedu
 

Mehr von mrecedu (20)

Brochure final
Brochure finalBrochure final
Brochure final
 
Unit i
Unit iUnit i
Unit i
 
Filters unit iii
Filters unit iiiFilters unit iii
Filters unit iii
 
Attenuator unit iv
Attenuator unit ivAttenuator unit iv
Attenuator unit iv
 
Two port networks unit ii
Two port networks unit iiTwo port networks unit ii
Two port networks unit ii
 
Unit 8
Unit 8Unit 8
Unit 8
 
Unit5
Unit5Unit5
Unit5
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
 
Unit2 jwfiles
Unit2 jwfilesUnit2 jwfiles
Unit2 jwfiles
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfiles
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworld
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworld
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworld
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworld
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworld
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworld
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworld
 
M1 unit vii-jntuworld
M1 unit vii-jntuworldM1 unit vii-jntuworld
M1 unit vii-jntuworld
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Unit4

  • 1. C Data Types: Primary data types Derived Derived data types Types User-defined data types Function Pointer Structure Array Type Union Type Type Type 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-1 Book-2 Book-3 BookID: 1211 BookID: 1212 BookID: 1213 Title : C Primer Plus Title : The ANSI C Programming Title : C By Example Author : Stephen Prata Author : Dennis Ritchie Author : Greg Perry Pages : 984 Pages : 214 Pages : 498 Price : Rs. 585.00 Price : Rs. 125.00 Price : Rs. 305.00 book book_id integer 2 bytes bookid title Array of 50 characters 50 bytes title author Array of 40 characters 40 bytes author pages integer 2 bytes pages price price float 4 bytes Memory occupied by a Structure variable STRUCTURE- BOOK struct < structure_tag_name > struct book { { Structure tag data type < member 1 > int book_id ; char title[50] ; data type < member 2 > …. …. …. …. char author[40] ; data type < member N > int pages ; }; float price ; };
  • 3. Declaring a Structure Type Declaring a Structure Variable struct student struct student s1,s2,s3; { (or) int roll_no; struct student char name[30]; { float percentage; int roll_no; }; char name[30]; float percentage; }s1,s2,s3; Initialization of structure Reading values to members at runtime: Initialization of structure variable while declaration : struct student s3; struct student s2 = { 1001, “ K.Avinash ”, printf(“nEnter the roll no”); 87.25 } ; scanf(“%d”,&s3.roll_no); Initialization of structure members individually : printf(“nEnter the name”); s1. roll_no = 1111; scanf(“%s”,s3.name); strcpy ( s1. name , “ B. Kishore “ ) ; printf(“nEnter the percentage”); s1.percentage = 78.5 ; scanf(“%f”,&s3.percentage); membership operator
  • 4. Implementing a Structure struct employee { int empid; char name[35]; int age; Declaration of Structure Type float salary; }; Declaration of Structure variables int main() { struct employee emp1,emp2 ; Declaration and initialization of Structure variable struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ; emp1.empid=1211; strcpy(emp1.name, “K.Ravi”); emp1.age = 27; Initialization of Structure members individually emp1.salary=30000.00; printf(“Enter the details of employee 2”); Reading values to members of Structure scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary); if(emp1.age > emp2.age) printf( “ Employee1 is senior than Employee2n” ); else printf(“Employee1 is junior than Employee2n”); Accessing members of Structure printf(“Emp ID:%dn Name:%sn Age:%dn Salary:%f”, emp1.empid,emp1.name,emp1.age,emp1.salary); }
  • 5. Arrays And structures Nesting of structures struct student struct date { { int day ; int sub[3] ; int month ; Outer Structure int total ; int year ; }; }; struct person { char name[40]; int main( ) { int age ; struct student s[3]; struct date b_day ; int i,j; }; for(i=0;i<3;i++) { int main( ) { Inner Structure printf(“nnEnter student %d marks:”,i+1); struct person p1; for(j=0;j<3;j++) { strcpy ( p1.name , “S. Ramesh “ ) ; scanf(“%d”,&s[i].sub[j]); p1. age = 32 ; } p1.b_day.day = 25 ; Accessing Inner } p1.b_day. month = 8 ; Structure members for(i=0;i<3;i++) { p1.b_day. year = 1978 ; s[i].total =0; } for(j=0;j<3;j++) { s[i].total +=s[i].sub[j]; OUTPUT: } Enter student 1 marks: 60 60 60 printf(“nTotal marks of student %d is: %d”, Enter student 2 marks: 70 70 70 i+1,s[i].total ); 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
  • 6. structures and functions Self referential structures struct fraction { struct student_node { int numerator ; int roll_no ; int denominator ; char name [25] ; }; struct student_node *next ; }; void show ( struct fraction f ) int main( ) { { printf ( “ %d / %d “, f.numerator, struct student_node s1 ; struct student_node s2 = { 1111, “B.Mahesh”, NULL } ; f.denominator ) ; s1. roll_no = 1234 ; } strcpy ( s1.name , “P.Kiran “ ) ; int main ( ) { s1. next = & s2 ; s2 node is linked to s1 node struct fraction f1 = { 7, 12 } ; show ( f1 ) ; printf ( “ %s “, s1. name ) ; } Prints P.Kiran printf ( “ %s “ , s1.next - > name ) ; Prints B.Mahesh OUTPUT: } 7 / 12 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.
  • 7. Pointer to a structure struct product Accessing structure members through { pointer : int prodid; char name[20]; i) Using . ( dot ) operator : }; ( *ptr ) . prodid = 111 ; strcpy ( ( *ptr ) . Name, “Pen”) ; int main() { ii) Using - > ( arrow ) operator : struct product inventory[3]; ptr - > prodid = 111 ; struct product *ptr; strcpy( ptr - > name , “Pencil”) ; printf(“Read Product Details : n"); for(ptr = inventory;ptr<inventory +3;ptr++) { scanf("%d %s", &ptr->prodid, ptr->name); Read Product Details : } 111 Pen printf("noutputn"); 112 Pencil for(ptr=inventory;ptr<inventory+3;ptr++) 113 Book { printf("nnProduct ID :%5d",ptr->prodid); Print Product Details : printf("nName : %s",ptr->name); } 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; Memory allotted to structure student float avg ; char grade ; Address 5000 5001 5002 5003 5004 5005 5006 }; union pupil { int rollno; float avg ; rollno avg grade char grade; Total memory occupied : 7 bytes }; int main() { struct student s1 ; Memory allotted to union pupil union pupil p1; printf ( “ %d bytes “, Address 5000 5001 5002 5003 sizeof ( struct student ) ) ; printf ( “ %d bytes “, sizeof ( union pupil ) ) ; } rollno Output : avg 7 bytes 4 bytes grade Total memory occupied : 4 bytes
  • 9. www.jntuworld.com • For More Materials, Text Books, Previous Papers & Mobile updates of B.TECH, B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU- KAKINADA & JNTU-ANANTAPUR visit www.jntuworld.com
  • 10. www.jntuworld.com • For More Materials, Text Books, Previous Papers & Mobile updates of B.TECH, B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU- KAKINADA & JNTU-ANANTAPUR visit www.jntuworld.com