1
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAStructures and Unions
Structures are introduced to combine different data types into single block. We can access
entire block of data with the structure. It can store different data elements information as a single
unit.
Definition of Structure:
Structure is a user defined data type and it is a combination of different data types called as
members. All these members are referred by a single name known as structure name. Structure can
be placed before or within the main.
Syntax to Create Structure:
struct tagname {
data type member 1; …………..
…………..
data type member n; };
We can access the members of structures using structure variable. This structure variable
should be created after completion of structure definition. We can create structure variable in two
ways.
Structure variable can be created as follows:
Syntax: struct tagname(structurename) structure_variable_name;
Ex: struct student s;
We can also create the structure variable along with the structure definition as follows:
Syntax:
struct tagname {
data type member 1; …………..
…………..
data type member n; }structure variable name;
Accessing members of a Structure:
We can access members of structure by using dot (.) operator along with the structure
2
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCASyntax: structure_variable_name.member;
Ex: e.eno;
e.name;
e.sal;
The following program illustrates the structure with employee data:
Aim: To write a C-Program to read and print an employee data using structure.
Program:
#include<stdio.h>
#include<conio.h>
struct Employee
{
int eno;
char ename[20];
int esal;
};
void main()
{
struct Employee e;
clrscr();
printf("\nEnter Employee Number:");
3
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAprintf("\nEnter Employee Name:");
scanf("%s",&e.ename);
printf("\nEnter Employee Salaray:");
scanf("%d",&e.esal);
printf("\n\nThe Employee Details are....\n");
printf("Employee Number:%d",e.eno);
printf("\nEmployee Name:%s",e.ename);
printf("\nEmployee Salary:%d",e.esal);
getch();
}
Out put:
Array of Structures:
Array is a collection of similar data types. In the same way we can also define array of
structures. In this structure array, every element is of structure type. The array of structure is
declared as follows
Syntax: struct tagname {
Data type member1; Data type member2; --- --- }array variable name[3]; Ex: struct emp
{
4
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAIn the above example e[10] is an array of 10 elements containing 10 objects of type
structure. Each element of e[10] has structure type with three members that are eno,ename and
esal.
The following program illustrates the above concept.
Aim: To Write a C-Program that creates a structure of 10 students having student number
(sno), student name (sname), marks in three subjects (m1,m2,m3), total marks of the
students (tot) and average (avg) and display the names of failed students.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
struct Student
{
int sno;
char sname[20];
int m1,m2,m3;
int tot,avg;
};
int n,i;
struct Student s[10];
clrscr();
printf("\nEnter the Number of Students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d Student Details\n",i+1);
printf("Student Number:");
scanf("%d",&s[i].sno);
5
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAscanf("%s",&s[i].sname);
printf("Student Three Subjects Marks\n");
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
}
printf("\n\nThe failed students are...\n");
for(i=0;i<n;i++)
{
if(s[i].avg<35)
printf("%s\n",s[i].sname);
}
getch();
}
6
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAPointer to Structures:
Pointer is a variable can store the address of another variable .The variable may be of any
data type. In the same way we can also define pointer to structure. Here, starting address of the
member variables can be accessed. Thus, such pointers are called structure pointers.
Ex: Struct book
{
char name[25];
char author[25];
int pages;
};struct book *ptr;
In the above example *ptr is pointer to structure book. The syntax for using pointer with
member is as given below
1) ptr-> name 2) ptr -> author 3) ptr -> pages
Structure and Functions:
Like variables of standard data type structure variables also can be passed to the function by
value or address. The syntax for the same is as follows
Syntax:
struct tagname
{
Data type member1;
Data type member 2;
---
---
}Structure_variable_name;
void main() {
<return type> <function name>( struct tagname[], argument list);//Protoype of function ---
--- --- function name( structure variable, argument list);//Function caller }
7
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA{
--- --- --- }
Example:
struct book {
char name[20]; char author[20]; int pages; }b1;
void main() {
void show(struct book);//Prototype ---
---
show(b1);//Caller ---
--- }
void show(struct book b2)//Callee {
--- --- }
Whenever a structure element is to be passed to any other function, it is essential to declare
the structure outside the main() function i.e., global.
The following program illustrates the above concept.
Aim: To write a C program to find the sum of two complex numbers.
Program:
#include<stdio.h>
#include<conio.h>
struct Complex
{
int real;
int img;
};
void f1(struct Complex x1,struct Complex x2)
8
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAstruct Complex x3;
x3.real=x1.real + x2.real;
x3.img=x1.img + x2.img;
printf("\n The Complex Sum is: %d+i%d",x3.real,x3.img);
}
void main()
{
struct Complex c1,c2;
clrscr();
printf("\nEnter the real and imag parts of first Complex Number:\n");
scanf("%d%d",&c1.real,&c1.img);
printf("\nEnter the real and img parts of second Complex Number:\n");
scanf("%d%d",&c2.real,&c2.img);
f1(c1,c2);
getch();
}
Output:
Unions:
A union is a user defined data type available in C that enables us to store different data
types in the same memory location. We can define a union with many members, but only one
member can contain a value at any given time. Unions provide an efficient way of using the same
9
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCADefining a Union
To define a union, we must use the union statement. The format of the union statement is
as follows:
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
Example:
union Data
{
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of
characters.
The memory occupied by a union will be large enough to hold the largest member of the
union. For example, in above example Data type will occupy 20 bytes of memory space because
this is the maximum space which can be occupied by character string.
Following is the example which will display total memory size occupied by the above
union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
10
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
When the above code is compiled and executed, it produces the following result:
Memory size occupied by data : 20
Accessing Union Members
To access any member of a union, we use the member access operator (.).
Following is the example to explain usage of union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
11
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAWhen the above code is compiled and executed, it produces the following result:
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that values of i and f members of union got corrupted because final value
assigned to the variable has occupied the memory location and this is the reason that the value of
str member is getting printed very well.
Now let's look into the same example once again where we will use one variable at a time
which is the main purpose of having union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 10
12
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAdata.str : C Programming
Enumerated Data types in C:
Enumeration type allows programmer to define their own data type . Keyword enum is
used to defined enumerated data type.
enum type_name{ value1, value2,...,valueN };
Here, type_name is the name of enumerated data type or tag and value1, value2,....,valueN
are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can
change the default value as below:
enum suit{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
Declaration of enumerated variable
The above code defines the type of the data but, no variable is created for the enum data
type. The following will shows the creation of a variable of type enum.
enum boolean{
false;
true;
};
enum boolean check;
Here, a variable check is declared which is of type enum boolean.
Example of enumerated type
#include <stdio.h>
enum week
{
sunday, monday, tuesday, wednesday, thursday, friday, Saturday
};
13
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAenum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Output
4 day
We can write any program in C language without the help of enumerations but,
enumerations helps in writing clear codes and simplify programming.
Explain the Scope and Visibility of Variables in C
A scope in any programming is a region of the program where a defined variable can have
its existence and beyond that the variable cannot be accessed. There are three places where
variables can be declared in C programming language:
Inside a function or a block which is called local variables,
Outside of all functions which is called global variables.
In the definition of function parameters which is called formal parameters. Let us explain what are local and global variables and formal parameters.
Local Variables
Variables that are declared inside a function or block are called local variables. They can
be used only by statements that are inside that function or block of code. Local variables are not
known to functions outside their own. Following is the example using local variables. Here all the
variables a, b and c are local to main() function.
#include <stdio.h>
int main ()
{
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10;
14
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAc = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Global Variables
Global variables are defined outside of a function, usually on top of the program. The
global variables will hold their value throughout the lifetime of a program and they can be
accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. Following is the example using global
and local variables:
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
A program can have same name for local and global variables but value of local variable
inside a function will take preference. Following is an example:
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
15
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA/* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of g = 10
Formal Parameters
Function parameters, formal parameters, are treated as local variables with-in that function
and they will take preference over the global variables. Following is an example:
#include <stdio.h>
/* global variable declaration */
int a = 20;
int main ()
{
/* local variable declaration in main function */
int a = 10;
int b = 20;
int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}
/* function to add two integers */
int sum(int a, int b)
{
printf ("value of a in sum() = %d\n", a);
printf ("value of b in sum() = %d\n", b);
return a + b;
16
B.B.B.B. Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCANaresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAWhen the above code is compiled and executed, it produces the following result:
value of a in main() = 10
value of a in sum() = 10
value of a in sum() = 20
value of b in main() = 30
Important Questions:
Define structure and explain the process of accessing structure variables with an example.
Explain array of structures with an example.
Explain the pointers to structures with an example.
Explain how to pass structures to functions with an example.
Explain about the Unions in C with an example.
Explain about the Enumerated data types in C.