Engaging Peers, Inspiring Careers! FaaDoOEnginee rs.com India’s No.1 website for: IIT-JEE/AIEEE preparation resources Coaching centre packages Engineering.

Slides:



Advertisements
Similar presentations
#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Advertisements

Problem Solving & Program Design in C
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
CSC141- Introduction to Computer Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
C Language.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Module 4: Structures ITEI222 Advanced Programming.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Array, Structure and Union
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
UNION UNIT -II. Unions A union is another compound datatype like a structure. So what is the properties of structure? Eg: Union exam { int roll_no; Char.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
STRUCTURES. Library system : Book’s name (a string), Book’s price (a float) number of pages in it (an int). SOLUTION- Construct individual arrays, one.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Structures, Unions, Enumerations
Chapter 10-1: Structure.
Structures and Unions.
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
Basic notes on pointers in C
DATA HANDLING.
Buy book Online -
Chapter 1: Introduction to Data Structures(8M)
Chapter 10 Structures and Unions
Structures In C Programming By Rajanikanth B.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

Engaging Peers, Inspiring Careers! FaaDoOEnginee rs.com India’s No.1 website for: IIT-JEE/AIEEE preparation resources Coaching centre packages Engineering Major and Minor projects Seminar reports Paper presentations EBOOKS Resumes/CVs, and so much more…

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 Book-2 BookID: 1212 Title : The ANSI C Programming Author : Dennis Ritchie Pages : 214 Price : Rs Book-3 BookID: 1213 Title : C By Example Author : Greg Perry Pages : 498 Price : Rs 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 { data type …. …. …. …. data type } ; Memory occupied by a Structure variable

Initialization of structure Initialization of structure variable while declaration : struct student s2 = { 1001, “ K.Avinash ”, } ; 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, } ; emp1.empid=1211; strcpy(emp1.name, “K.Ravi”); emp1.age = 27; emp1.salary= ; 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: Enter student 2 marks: Enter student 3 marks: 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 functionsSelf 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"); for(ptr = inventory;ptr<inventory +3;ptr++) { scanf("%d %s", &ptr->prodid, ptr->name); } printf("\noutput\n"); for(ptr=inventory;ptr<inventory+3;ptr++) { printf("\n\nProduct ID :%5d",ptr->prodid); printf("\nName: %s",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 rollno avg grade Total memory occupied : 7 bytes Memory allotted to union pupil rollno avg grade Total memory occupied : 4 bytes Address

Engaging Peers, Inspiring Careers! FaaDoOEnginee rs.com India’s No.1 website for: IIT-JEE/AIEEE preparation resources Coaching centre packages Engineering Major and Minor projects Seminar reports Paper presentations EBOOKS Resumes/CVs, and so much more…