UNIT 4 Part-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Structures in C

In C programming language, a structure is a collection of elements of the different data type.


The structure is used to create user-defined data type in the C programming language. As the
structure used to create a user-defined data type, the structure is also said to be “user-defined
data type in C”.
In other words, a structure is a collection of non-homogeneous elements. Using structure we
can define new data types called user-defined data types that holds multiple values of the
different data type. The formal definition of structure is as follows...
Definition:
Structure is a collection of different type of elements under a single name that acts as user
defined data type in C.
Generally, structures are used to define a record in the c programming language. Structures
allow us to combine elements of a different data type into a group. The elements that are defined
in a structure are called members of structure.
How to create structure?
To create structure in c, we use the keyword called "struct". We use the following syntax to
create structures in c programming language.
struct <structure_name>
{
data_type member1;
data_type member2, member3;
.
.
};
Following is the example of creating a structure called Student which is used to hold student
record.
Creating structure in C
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
};
Creating and Using structure variables
In a c programming language, there are two ways to create structure variables. We can create
structure variable while defining the structure and we can also create after terminating structure
using struct keyword.
To access members of a structure using structure variable, we use dot (.) operator. Consider the
following example code...
Creating and Using structure variables in C
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} stud_1 ; // while defining structure

void main(){
struct Student stud_2; // using struct keyword
printf("Enter details of stud_1 : \n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);

printf("***** Student 1 Details *****\n);


printf("Name of the Student : %s\n", stud_1.stud_name);
printf("Roll Number of the Student : %i\n", stud_1.roll_number);
printf("Percentage of the Student : %f\n", stud_1.percentage);
}
In the above example program, the stucture variable "stud_1 is created while defining the
structure and the variable "stud_2 is careted using struct keyword. Whenever we access the
members of a structure we use the dot (.) operator.
Memory allocation of Structure
When the structures are used in the c programming language, the memory does not allocate on
defining a structure. The memory is allocated when we create the variable of a particular
structure. As long as the variable of a structure is created no memory is allocated. The size of
memory allocated is equal to the sum of memory required by individual members of that
structure. In the above example program, the variables stud_1 and stud_2 are allocated with 36
bytes of memory each.

Unions in C
In C programming language, the union is a collection of elements of the different data type.
The union is used to create user-defined data type in the C programming language. As the union
used to create a user-defined data type, the union is also said to be “user-defined data type in
C”.
In other words, the union is a collection of non-homogeneous elements. Using union we can
define new data types called user-defined data types that holds multiple values of the different
data type. The formal definition of a union is as follows...
Definition:
Union is a colloction of different type of elements under a single name that acts as user defined
data type in C.
Generally, unions are used to define a record in the c programming language. Unions allow us
to combine elements of a different data type into a group. The elements that are defined in a
union are called members of union.
How to create union?
To create union in c, we use the keyword called "union". We use the following syntax to create
unions in c programming language.
union <union_name>
{
data_type member1;
data_type member2, member3;
.
.
};
Following is the example of creating a union called Student which is used to hold student
record.
Creating union in C
union Student
{
char stud_name[30];
int roll_number;
float percentage;
};

Creating and using union variables


In a c programming language, there are two ways to create structure variables. We can create
structure variable while defining the structure and we can also create after terminating structure
using struct keyword.
To access members of a structure using structure variable, we use dot (.) operator. Consider the
following example code...
Creating and Using union variables in C
union Student
{
char stud_name[30];
int roll_number;
float percentage;
} stud_1 ; // while defining structure

void main(){
union Student stud_2; // using struct keyword
printf("Enter details of stud_1 : \n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);

printf("***** Student 1 Details *****\n);


printf("Name of the Student : %s\n", stud_1.stud_name);
printf("Roll Number of the Student : %i\n", stud_1.roll_number);
printf("Percentage of the Student : %f\n", stud_1.percentage);
}
In the above example program, the union variable "stud_1 is created while defining the union
and the variable "stud_2 is careted using union keyword. Whenever we access the members of
a union we use the dot (.) operator.
Memory allocation of Union
When the unions are used in the c programming language, the memory does not allocate on
defining union. The memory is allocated when we create the variable of a particular union. As
long as the variable of a union is created no memory is allocated. The size of memory allocated
is equal to the maximum memory required by an individual member among all members of
that union. In the above example program, the variables stud_1 and stud_2 are allocated with
30 bytes of memory each.
Bit Fields in C
When we use structures in the c programming language, the memory required by structure
variable is the sum of memory required by all individual members of that structure. To save
memory or to restrict memory of members of structure we use bit field concept. Using bit field
we can specify the memory to be allocated for individual members of a structure. To understand
the bit fields, let us consider the following example code...
Date structure in C
struct Date
{
unsigned int day;
unsigned int month;
unsigned int year;
};

Here, the variable of Date structure allocates 6 bytes of memory.


In the above example structure the members day and month both does not requires 2 bytes of
memory for each. Becuase member day stores values from 1 to 31 only which requires 5 bits
of memory, and the member month stores values from 1 to 12 only which required 4 bits of
memory. So, to save the memory we use the bitfields.
Consider the following structure with bitfields...
struct Date
{
unsigned int day : 5;
unsigned int month : 4;
unsigned int year;
};
Here, the variable of Date structure allocates 4 bytes of memory.

typedef in C
In C programming language, typedef is a keyword used to create alias name for the existing
datatypes. Using typedef keyword we can create a temporary name to the system defined
datatypes like int, float, char and double. we use that temporary name to create a variable. The
general syntax of typedef is as follows...
typedef <existing-datatype> <alias-name>;
typedef with primitive datatypes
Consider the following example of typedef
typedef int Number
In the above example, Number is defined as alias name for integer datatype. So, we can use
Number to declare integer variables.

Example Program to illustrate typedef in C.


#include<stdio.h>
#include<conio.h>

typedef int Number;

void main(){

Number a,b,c; // Here a,b,&c are integer type of variables.

clrscr() ;
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &a,&b) ;

c = a + b;

printf("Sum = %d", c) ;
}

typedef with Arrays

In C programming language, typedef is also used with arrays. Consider the following
example program to understand how typedef is used with arrays.

Example Program to illustrate typedef with arrays in C.

#include<stdio.h>
#include<conio.h>

void main(){

typedef int Array[5]; // Here Array acts like an integer array type of size 5.

Array list = {10,20,30,40,50}; // List is an array of integer type with size 5.


int i;

clrscr() ;

printf("List elements are : \n") ;

for(i=0; i<5; i++)


printf("%d\t", list[i]) ;
}
In the above example program, Array is the alias name of integer array type of size 5. We can
use Array as datatype to create integer array of size 5. Here, list is an integer array of size 5.

typedef with user defined datatypes like structures, unions etc.,


In C programming language, typedef is also used with structures and unions. Consider the
following example program to understand how typedef is used with structures.

Example Program to illustrate typedef with arrays in C.


#include<stdio.h>
#include<conio.h>
typedef struct student
{
char stud_name[50];
int stud_rollNo;
}stud;

void main(){
stud s1;
clrscr() ;
printf("Enter the student name: ") ;
scanf("%s", s1.stud_name);
printf("Enter the student Roll Number: ");
scanf("%d", &s1.stud_rollNo);
printf("\nStudent Information\n");
printf("Name - %s\nHallticket Number - %d", s1.stud_name, s1.stud_rollNo);
}
In the above example program, stud is the alias name of student structure. We can use stud as
datatype to create variables of student structure. Here, s1 is a variable of student structure
datatype.

You might also like