Definition, Declaration of Structures in C.pptx

AnithaTAssistantProf 243 views 18 slides Mar 03, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.


Slide Content

V.V. Vanniaperumal college for women Programming in C Structures Dr. T. Anitha Assistant Professor Department of Mathematics(SF)

int dynami ; int trig; int c; float lab;

An array is a fixed-size sequenced collection of elements of same data type. ie ., it is a grouping of like-type. So in array a list of items can be given in one variable name. The general form of array declaration is type variable_name [size] Examples: int dynami [50]; int trig[50]; int c[50]; float lab[40]; Array

Example: Suppose we have to store prices of three different items in four shops: Item 1 Item 2 Item 3 Shop 1 310 275 365 Shop 2 210 190 325 Shop 3 405 225 240 Shop4 260 300 380

C allows as to define such tables of item in single variable using two-dimensional arrays such as p[4][3] Two-dimensional array are declared as follows: Type array_name [ row_size ][ column_size ]

Structure is a mechanism for packing data of different data type. A structure is a convenient tool for handling a group of related data item. E xample: Using structure we can represent a set of attributes, such as student_name , roll_number and marks. Structures in C

struct book_bank { char title[20]; char author[15]; int page; Float price; }; Definition of Structures

T he keyword struct declares a structure to hold the details of four data field, namely title, author, pages and price, these fields are called structure elements or members . Note that each member may belong to different data types. book-bank is the name of the structure which called structure tag .

s truct tag_name { d ata_type member1; data_type member2; data_type member; --------- }; The general format of structure definition

Declaring Structure Variables After defining a structure we can declare variables of that type. A structure variable declarations is similar to declaration of variables of any other data types. It includes the following elements: The keyword strut The structure tag name. List of variable names separated by commas. A terminating semicolon. Example: struct book_bank , book1, book2, book3; It declares book1, book2, book3 are variables of type struct book_bank .

Declaring Structure Variables The complete variable definition and declaration is struct book_bank { char title[20]; char author[15]; int page; Float price; }; struct book_bank , book1, book2, book3;

Declaring Structure Variables It is also allowed to combine both variable definition and variable declaration in one step as: struct book_bank { char title[20]; char author[15]; int page; float price; } book1, book2, book3;

Access Structure Members To access members of a structure, use the member operator dot operator (.): Example: book1.price is the variable representing the price of book1 and it is treated like any other ordinary variable.

Assigning values to the Structure variables b ook.page =250; book1.price=120.5 Strcpy (book1.title,”Algebra”); Strcpy (book1.author, “ Arumugam ”);

Assigning values to the Structure variables Reading data from keyboard We can use scanf to give the values through the keyboard Scanf (“%s\n”,&book1.title); Scanf (“%d\n”, &book1.pages);

Example Program struct personal { char name[20]; int day; char month[10]; float salary; }; m ain() { structure personal person; printf (“Input Values\n”); scanf (“%s %d %s %f”, person.name,&person.day , & person.month,&person.salary ); printf (“Output Values\n”); printf (“%s %d %s % f”, person.name,person.day , person.month,person.salary ); }

Output: Input Values Rani 10 January 25000 Output Values Rani 10 January 25000

Thank you