C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
2. Outline
What is a Structure?
Defining a Structure
General format of a Structure
Arrays vs. Structure
Accessing Structure Members
Rules for initializing Structures
What is a Union?
Difference between Structure and Union
3. What is a Structure?
We can not use an array if we want to represent a
collection of data items of different types using a
single name.
Structures are a mechanism for packing data of
different types.
A structure is a convenient tool for handling a group
of logically related data items.
4. What is a Structures? (cont..)
Examples of structures:
time : seconds, minutes, hours
date : day, month, year
book : author, title, price, year
city : name, country, population
Structures help to organize complex data in a more
meaningful way
5. Defining a Structure
Consider a book database consisting of book name, author,
number of pages, and price.The structure to hold this
information as follows:
struct book_name
{
char title[20];
char author[15];
int pages ;
float price;
}
6. Defining a Structure (cont..)
The keyword struct declares a structure to hold the
details of four data fields, namely title, author,
pages, and price.These fields are called structure
elements or members.
Each member may belong to a different type of data.
book_name is the name of the structure and is
called the structure tag.
9. Arrays vs. Structure
1. An array is a collection of related data elements of
same types. Structure can have elements of different
types.
2. An array is derived data type whereas a structure is
a programmer-defined one.
3. Any array behaves like a built-in data type.All we
have to do is to declare an array variable and use it.
But in the case of a structure, first we have to
design and declare a data structure before the
variables of that type are declared and used.
10. Declaring a Structure
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
} book1, book2, book3;
Here book1, book2, book3 are declared as variables
of type struct book_bank .
11. Accessing Structure Members
The link between a member and a variable is
established using the member operation ('.’)
From the previous example:
Book1.price
It is a variable representing the price of book1.
We can use scanf to give the value through keyboard
scanf(“%s”, book1.title);
scanf(“%d”, &book1.pages);
12. Accessing Structure Members (Code)
struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main()
{
struct personal person;
printf(“InputValue: ”);
14. Rules for initializing Structures
1. We cannot initialize individual members inside the structure
template.
2. The order of values enclosed in braces must match the order of
members in the structure definition.
3. It is permitted to have a partial initialization.We can initialize only
the first few members and leave the remaining blank.The
uninitialized members should be only at the end of the list.
4. The uninitialized members will be assigned default values as
follows:
Zero for integer and floating point numbers.
‘0’ for characters and strings.
15. What is a Union?
Unions are a concept borrowed from structures and
therefore follow the same syntax as structures.
The major distinction between structure and union is
the storage.
In structure, each member has its own storage
location, whereas all the members of a union use the
same location.
Although a union may contain many members o
different types, it can handle only one member at a
time.
16. Unions (Example1)
For example in the following C program, both x and y share the same location. If we change x, we can
see the changes being reflected in y.
– union test {
– int x, y;
– };
– int main()
– {
– union test t;
– t.x = 2; // t.y also gets value 2
– printf("After making x = 2:n x = %d, y = %dnn", t.x, t.y);
– t.y = 10; // t.x is also updated to 10
– printf("After making y = 10:n x = %d, y = %dnn", t.x, t.y);
– return 0;
– }
17. Unions (Example1) (cont..)
Output:
– After making x = 2:
– x = 2, y = 2
– After making y = 10:
– x = 10, y = 10