Professional Documents
Culture Documents
CS 174: PROGRAMMING IN C
Masoud H. Mahundi
Structures and Unions
Introduction
Appropriate when we need to manage data of different data type
Students
name, registration number, date of birth
Address
house number, street, zip code, country
Books
Author, pages, ISBN, Tittle, year
Structure - Definition
Structure Definition is basically creating a temporary data type to later be used in
declaration
Definition - you specify the different data types included in structure data type
struct structure_name
{
data_type member1;
data_type member2;
……..
data_type memberN;
};
struct books
{
char title[20]; Data type: struct books
char author[15];
int pages;
float price;
};
struct address {
unsigned int house_number;
char street_name[50];
Data type: struct address
int zip_code;
char country[50];
};
Structure - Declaration
1. Immediately after definition
struct books struct books
{ {
char title[20]; char title[20];
char author[15]; char author[15];
int pages; int pages;
float price; float price;
} book1; } book1, book2;
book1.author = “mahundi”;
struct student_some_detail
{
int college_id; Write scanf statements to
char college_name[50];
access all the members of
};
stu_data variable
struct student_detail
{
int id;
char name[20];
float percentage;
struct student_some_detail clg_data;
}stu_data;
Structure - Arrays
As a normal array
struct student
Data type being the structure defined {
char reg_no[12];
int values[5];
float allowance;
float marks[200]; char sex;