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.
Example:
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
The 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.
struct tag_name
{
data_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
book.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(“%sn”,&book1.title);
Scanf(“%dn”, &book1.pages);
Example Program
struct personal
{
char name[20];
int day;
char month[10];
float salary;
};
main()
{
structure personal person;
printf(“Input Valuesn”);
scanf(“%s %d %s %f”, person.name,&person.day, &person.month,&person.salary);
printf(“Output Valuesn”);
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

Definition, Declaration of Structures in C.pptx

  • 1.
    V.V. VANNIAPERUMAL COLLEGEFOR WOMEN Programming in C Structures Dr. T. Anitha Assistant Professor Department of Mathematics(SF)
  • 2.
     int dynami; int trig;  int c;  float lab;
  • 3.
    An array isa 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
  • 4.
    Example: Suppose we haveto 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
  • 5.
    C allows asto 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]
  • 6.
    Structure is amechanism for packing data of different data type. A structure is a convenient tool for handling a group of related data item. Example: Using structure we can represent a set of attributes, such as student_name, roll_number and marks. Structures in C
  • 7.
    struct book_bank { char title[20]; charauthor[15]; int page; Float price; }; Definition of Structures
  • 8.
    The keyword structdeclares 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.
  • 9.
    struct tag_name { data_type member1; data_typemember2; data_type member; --------- }; The general format of structure definition
  • 10.
    Declaring Structure Variables Afterdefining 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.
  • 11.
    Declaring Structure Variables Thecomplete variable definition and declaration is struct book_bank { char title[20]; char author[15]; int page; Float price; }; struct book_bank, book1, book2, book3;
  • 12.
    Declaring Structure Variables Itis 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;
  • 13.
    Access Structure Members Toaccess 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.
  • 14.
    Assigning values tothe Structure variables book.page=250; book1.price=120.5 Strcpy(book1.title,”Algebra”); Strcpy(book1.author, “Arumugam”);
  • 15.
    Assigning values tothe Structure variables Reading data from keyboard We can use scanf to give the values through the keyboard Scanf(“%sn”,&book1.title); Scanf(“%dn”, &book1.pages);
  • 16.
    Example Program struct personal { charname[20]; int day; char month[10]; float salary; }; main() { structure personal person; printf(“Input Valuesn”); scanf(“%s %d %s %f”, person.name,&person.day, &person.month,&person.salary); printf(“Output Valuesn”); printf (“%s %d %s %f”, person.name,person.day, person.month,person.salary); }
  • 17.
    Output: Input Values Rani 10January 25000 Output Values Rani 10 January 25000
  • 18.