0% found this document useful (0 votes)
15 views

Unit V

C programming

Uploaded by

chandram.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit V

C programming

Uploaded by

chandram.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1

UNIT V
STRUCTURES

Derived types- structures- declaration, definition and initialization of structures, accessing


structures, nested structures, arrays of structures, structures and functions, pointers to
structures, self referential structures, unions, typedef, bit-fields, program applications BIT-
WISE OPERATORS: logical, shift, rotation, masks.

SAMPLE QUESTIONS

1) Explain about structures- declaration, definition and initialization of structures,


accessing structures?
2) Explain about nested structures?
3) Explain about arrays of structures?
4) Explain about structures and functions?
5) Explain about pointers to structures?
6) Explain about self referential structures?
7) Explain about unions?
8) Explain about typedef?
9) Explain about bit-fields?
10) Explain about Bit-Wise-Operators?
2

Definition:- The collection of similar (or ) different data-type items or values is called
Structure.

Structure Declaration:- Specifies the structure members is called Structure declaration.


Syntax:- struct structureName Example:-
{ struct StudentDetails
data-type member1; {
data-type member2; int rollno;
data-type member3; char name[20];
--------------------------------- float per;
---------------------------------- };
data-type member n;
};
 Structure is created by created by using the keyword called ‘struct’.
 Structure name is the user-defined and it is also called as ‘structure tag'.
 Structure declaration is starts with ‘{‘ and closed with ‘}’ followed by semicolon(;).
 The variables which are placed in-between curly-braces are called structures
members.

Structure Definition:- Creating the structure variable is called structure definition.


At the time of structure definition memory is allocated for structure members but not at the
time of structure declaration.
Syntax:- Example:-
struct structureName var1,var2,....,varN;
struct StudentDetails x , y ;

Accessing structures:- Accessing structure means accessing the structure members. That
means storing the values into structure members and displaying the values from structure
members.
The dot(.) operator is used for accessing the structure members. It forms the link between
structure variable and it’s member.
Syntax:- structure variable . member ;

Example:-
x.rollno=1;
strcpy(x.name,"hari");
x.per=78.23;
3

Write a C program to explain the concepts of structure declaration , definition and


accessing structure members.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct StudentDetails
{
int rollno;
char name[20];
float per;
};
void main()
{ struct StudentDetails x;
clrscr();
x.rollno=1;
strcpy(x.name,"hari");
x.per=78.23;
printf("Student Details:\n");
printf("Roll Number=%d Name=%s Percentage=%f",x.rollno,x.name,x.per);
getch();
}
Output:-
Roll Number=1 Name=hari Percentage=78.23

Structure Intialization: -
Values are assigned at the time of structure defitnion is called Structure Initialization.
Syntax:- struct structureName var1={list of values},var2={list of values},....,
var n={list of values};
Example :- struct StudentDetails x={1,”hari”,75.15},y={2,”siva”,78.25};

Write a C program to explain the concepts Structure Initialization.


#include<stdio.h>
#include<conio.h>
#include<string.h>
struct StudentDetails
{
int rollno;
char name[20];
float per;
};
void main()
{ struct StudentDetails x={1,"hari",78.23};
clrscr();
printf("Student Details:\n");
printf("Roll Number=%d Name=%s Percentage=%f",x.rollno,x.name,x.per);
getch();
}

Output:-
Roll Number=1 Name=hari Percentage=78.23
4

Pointers to structures
The variable which is the pointer for a structure is called pointer-to-structure. That means
here we are using the pointer as structure variable.
Syntax:-
struct StructureName *ptr1,*ptr2,......*ptr n;
Structure pointer Variable->Member;
We can access the structure members by the pointers using arrow operators arrow(->)
Example:-
struct StudentDetails
{
int rollno;
char name[20];
float per;
};

struct StudentDetails *xp,x;


Write a C-program to explain the concept of Pointer to structure.
#include<stdio.h>
#include<conio.h>
struct StudentDetails
{
int rollno;
char name[20];
float per;
};
void main( )
{
stuct StudentDetails x={10,”sai”,74,75},*xp;
clrscr();
xp=&x;
printf(“RollNo=%d”,xp->rollno);
printf(“Name=%s”,xp->name);
printf(“percentage=%f”,xp->per);
getch();
}
Output:-
RollNo=10
Name=sai
Percentage=74.75
5

Nested Structure: -Nested Structure means the structure which contains the another
structure variable as it’s member.
The diagrammatical representation of nested structure is ,

Consider the following example,

struct date
{
int day;
int mon;
int year;
};
struct StudentDetails
{
int rollno;
char name[20];
float per;
struct date dob;
};
struct StudentDetails x ;

 In the above example , StudentDetails structure contains the date structure variable
as it’s member. It is called Nested Structures.
 Members of the nested structure is accessed by using the following format.

External structure variable . Internal structure variable.Member ;

In the above example StudentDetails is the External structure and date is the internal
structure. Now we can access the internal structure members as follows:
x.dob.day=3;
x.dob.mon=8;
x.dob.year=1998;
6

Write a C program to explain the concept of Nested Structures.


#include<stdio.h>
#include<conio.h>
#include<string.h>
struct date
{
int day;
int mon;
int year;
};
struct StudentDetails
{
int rollno;
char name[20];
float per;
struct date dob;
};
void main( )
{ struct StudentDetails x;
clrscr( );
x.rollno=1;
strcpy(x.name,"hari");
x.per=78.23;
x.dob.day=3;
x.dob.mon=8;
x.dob.year=1998;
printf("Student Details:\n");
printf("Roll Number=%d Name=%s Percentage=%f
DOB=%d/%d/%d",x.rollno,x.name,x.per,x.dob.day,x.dob.mon,x.dob.year);
getch( );
}

Output:-

Student Details:
Roll Number=1 Name=hari Percentage=78.23
7

arrays of structures:-
 Array of structure means assigning size to the structure variable.
 Consider the following example:
Example: sttuct StudentDetails x[5];
Syntax : struct StructureName var[size];
 Consider the following explanation for what is the need to implement Array of
structures.
For displaying the 5 student details we need 5 structure variables as,
sttuct StudentDetails x,y,z,w,t;
 But by using the array of structure we can display the 5 student details using only one
structure variable as,
sttuct StudentDetails x[5];

Write a C program to explain the concept of array of structures.


#include<stdio.h>
#include<conio.h>
struct StudentDetails
{
int rollno;
char name[20];
float per;
struct date dob;
};
void main( )
{ int i;
struct StudentDetails x[3];
clrscr( );

printf(“Enter 3 student details:\n”);


for(i=0;i<3;i++)
{scanf(“%d%s%f” ,&x[i].rollno,x[i].name,&x[i].per);
}

printf(“ 3 student details:\n”);


for(i=0;i<3;i++)
{printf(“Roll no=%d Name=%s Percentage=%f\n” ,x[i].rollno,x[i].name,x[i].per);
}
getch();
}

Output:-
Enter 3 student details:
1 hari 78.23
2 siva 58.32
3 sree 65.32
3 student details:
Roll no=1 Name=hari Percentage=78.23
Roll no=2 Name=siva Percentage=58.32
Roll no=3 Name=sree Percentage=65.32
8

Typedef:-
Type definition allows us to renaming the existing built-in data-types and user-defined data-
types.The main purpose of the typedef is increasing the readability of the programming code.

Syntax:- typedef data-type newname;

Ex1 :- typedef int civil;


civil x,y,sum;

Ex2 :- sturct StudentDetails civil;


civil x,y,z;
Write a C program to perform sum of two numbers using typedef.
#include<stdio.h>
#include<conio.h>
void main( )
{ typedef int civil;
civil x,y,sum;
clrscr( );
x=10;
y=20;
sum=x+y;
printf(“sum is :%d”,sum);
getch( );
}
Output:-
sum is:30
Structures and functions
The following are the three possibilities to the structure members from calling function to
called function.
1)Passing members of a structure individually
2) Passing address of a structure variable and
3) Passing entire structure at once.
consider the following example:
struct StudentDetails
{
int rollno;
char name[20];
float per;
};
struct StudentDetails x;

1.passing structure members individually:-


display (x.rollno, x.name, x,per);/*calling fun*/
void display (int rollno, char name[ ], float per) /*called fun*/
{-------------
-----------------
----------------
}
9

2.passing entire structure at once:-

display(x);/*calling function*/

void display (struct StudentDetails x)/*called function*/


{-------------
-----------------
----------------
}

3.passing address of a structure variable:-

display(&x);/*calling function*/

void display(struct StudentDetails *x)/*called function */


{-------------
-----------------
----------------
}
Write a C program for passing entire structure at once.
#include <stdio.h>
#include <conio.h>
void display(struct StudentDetails x);
struct StudentDetails
{
int rollno;
char name[20];
float per;
};

void main()_
{
struct StudentDetails x;
clrscr();
x.rollno=10;
strcpy(x.name,”sai”);
x.per=96.73;
display(x);
getch();
}

void display(struct StudentDetails x)


{
printf(“%d%s%f” , x.rollno, x.name, x.per);
}

Output:-

10 sai 96.73
10

Bit-fields
The collection of adjacent bits in a word of memory is called Bit-fields. The main purpose of
the bit-field is to utilize the memory in effective manner that means wastage of memory is
eliminated by specifying the size.

Syntax:- struct structureName Example:-

{ struct StudentDetails
data-type member1:size; {
data-type member2:size; int rollno:7;
data-type member3:size; char name[20];:32
--------------------------------- float per:6;
---------------------------------- };
data-type member n:size;
};

 Consider the following explanation:

Stuct member memory allocation memory allocation in case


normals (Bytes) of bit fields (Bytes)

Roll no 2 1

Name 20 4

Per 4 1

Total 26 6

 By the above table we can say that for performing a task using bit fields, it needs 6B
instead of 26B.So,20B of memory wastage is reduced.
 The diagrammatical representation of memory using Bit-field is shown below

Write a C-program to explain the bit-fields.


#include<stdio.h>
#include<conio.h>
struct StudentDetails
{
int rollno:7;
char name[20];:32
float per:6;
};
11

void main()
{ struct StudentDetails x;
int length;
clrscr();
length=size of (x);

x.rollno=1;

strcpy(x.name,"hari");
x.per=78.23;

printf("Student Details:\n");
printf("Roll Number=%d Name=%s Percentage=%f\n",x.rollno,x.name,x.per);
printf(“no of bytes=%d”,length);

getch();
}
Output:-

Roll no=1 Name=hari percentage=78.23

No.of Bytes=6.

Union
Union is similar to structure that means it is also defined as, the Collection of the different
(or) similar data items The major different between union and structure is, in case of
structure the number of memory locations allocated is equal to the no. of structure members.
But in case of union only one memory location is allocated whose capacity is equal to the
largest data-type union member, but this memory is accessed by any union member one at a
time.

Union declaration:-

Syntax:- union UnionName Example:-

{ union StudentDetails
data-type member1; {
data-type member2; int rollno;
data-type member3; char name[20];
--------------------------------- float per;
---------------------------------- };
data-type member n;
};
Union Definition:-
Syntax:- union union-name var1,var2,,................,var n;
Ex:- union StudentDetails Praveen;
12

write a C program to understand the concept of union .


#include<stdio.h>
#include<conio.h>
union StudentDetails
{
int rollno;
char name[20];
float per ;
};
void main( )
{
union StudentDetails x;
Clrscr();
x.rollno=32;
printf(“%d ”,x.rollno);
strcpy(x.name,”naveen”);
printf(“% s ”,x.name);
x.per=93.75;
printf(“%f”,x.per);
getch();
}

Output:
32 naveen 93.75
13

BIT-WISE OPERATORS:-The operators which are performing operations at bit-level is


called bit-wise operator.Bitwise operators are ,

1 BIT-WISE AND (&)


2) BIT-WISE OR ( | )
3) BIT-WISE EXCLUSIVE OR(^)
4)BIT-WISE ONES COMPLEMENT(~):
5)SHIFT LEFT(<<)
6) SHIFT RIGHT(>>)

1 BIT-WISE AND (&):-

Syntax : Operand1 & operand2 ;

Table:

X Y X&Y

0 0 0

0 1 0

1 0 0

1 1 1

Ex: int x=5, y=4;

X in binary is : 0000 0000 0000 0101

Y in binary is : 0000 0000 0000 0100

X&Y : 0000 0000 0000 0100

2) BIT-WISE OR ( | ):-

Syntax : Operand1 | operand2 ;

Table:

X Y X|Y

0 0 0

0 1 1

1 0 1

1 1 1
14

Ex: int x=5,y=4;

X:0000 0000 0000 0101

Y:0000 0000 0000 0100

X|Y: 0000 0000 0000 0101

3) BIT-WISE EXCLUSIVE OR(^):-

Syntax : Operand1 ^ operand2 ;

Table:

X Y X^Y

0 0 0

0 1 1

1 0 1

1 1 0

Example: int x=5,y=4;

X : 0000 0000 0000 0101

Y : 0000 0000 0000 0100

X^Y: 0000 0000 0000 0001

4)BIT-WISE ONES COMPLEMENT(~):

Syntax : ~ operand;

Table:

X ~X

0 1

1 0

5)SHIFT LEFT(<<): it is used to shift the no.of bits of an integer towards left. The vacant
positions on the right of the integer would be filled with zeros.

Syntax:

Operand<<n; where ‘n’ is the no .of bits of operand to be shifted towards left .
15

Example:

int x=5;

x: 0000 0000 0000 0101

after x<<4 : 0000 0000 0101 0000

6)SHIFT RIGHT (>>):it is used to shift the no .of bits of an integer towards right. The
vacant positions on the left of the integer would be filled with zeros.

Syntax:

Operand>>n:, where ‘n’ is the no.of bits of operand to be shifted towards right.

Example: int x=5;

x: 0000 0000 0000 0101

After x>>2 : 0000 0000 0000 0001

Write a C program to explain the bitwise operators concept.


#include<stdio.h>
#include<conio.h>
void display(int n);
void main( )
{
int x , y , xory , xandy , xexory , xlshift , xrshift , n;
printf(“enter x and y values \n”);
scanf(“%d %d”,&x,&y);
xory=x|y;
xandy=x&y;
xexory=x^y;
printf(“Enter the no.of bits to be shifted \n”);
scanf(“%d”,&n);
xlshift=x<<n;
xrshift=x>>n;
printf(“x= “);display(x);
printf(“\n y= “);display(y);
printf(“\n xory= ”);display(xory);
printf(“\n xandy = “);disply(xandy);
printf(“\n xexory = “);disply(xexory);
printf(“\n xlshift= );dsplay (xlshift);
printf(“\n xrshift= ); display(xrshift);
getch();
}
16

void display(int n)
{
int i ,j,k;

for(i=15;i>=0;i--)
{
J=1<<i
k=j&n;
if(k>0)
printf(“1”);
else
printf(“0”);
}
}
Output
Enter x and y values
5 and 6
Enter the no.of bits to be shifted
5
x=0000 0000 0000 0101
y=0000 0000 0000 0110
xory=0000 0000 0000 0111
xandy=0000 0000 0000 0100
xexory=0000 0000 0000 0011
xlshift=0000 0000 0101 0000
xrshift=0000 0000 0000 0000

Write a C program to explain the bitwise operators concept.


#include<stdio.h>
#include<conio.h>
void display(int n);
void main( )
{
int x , y , xory , xandy , xexory , xlshift , xrshift , n;
printf(“enter x and y values \n”);
scanf(“%d %d”,&x,&y);
xory=x|y;
xandy=x&y;
xexory=x^y;
printf(“Enter the no.of bits to be shifted \n”);
scanf(“%d”,&n);
xlshift=x<<n;
xrshift=x>>n;
printf(“x= “);display(x);
printf(“\n y= “);display(y);
printf(“\n xory= ”);display(xory);
printf(“\n xandy = “);disply(xandy);
printf(“\n xexory = “);disply(xexory);
printf(“\n xlshift= );dsplay (xlshift);
17

printf(“\n xrshift= ); display(xrshift);


getch();
}
void display(int n)
{
int i ,j,k;
for(i=15;i>=0;i--)
{
J=1<<i
k=j&n;
if(k>0)
printf(“1”);
else
printf(“0”);
}
}
Output:-
Enter x and y values
5 and 6
Enter the no.of bits to be shifted
5
x=0000 0000 0000 0101
y=0000 0000 0000 0110
xory=0000 0000 0000 0111
xandy=0000 0000 0000 0100
xexory=0000 0000 0000 0011
xlshift=0000 0000 0101 0000
xrshift=0000 0000 0000 0000

You might also like