0% found this document useful (0 votes)
9 views183 pages

Programming in C - CS3251 2021 Regulation - Notes - Hand Writing

The document lists the subjects for the 2nd semester, including Professional English, Engineering Graphics, Statistics, various Physics courses, and several engineering-related subjects. It provides a comprehensive overview of the courses offered in this semester. The subjects cover a range of disciplines within engineering and technology.

Uploaded by

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

Programming in C - CS3251 2021 Regulation - Notes - Hand Writing

The document lists the subjects for the 2nd semester, including Professional English, Engineering Graphics, Statistics, various Physics courses, and several engineering-related subjects. It provides a comprehensive overview of the courses offered in this semester. The subjects cover a range of disciplines within engineering and technology.

Uploaded by

Goms Pandian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

All 2nd Semester Subjects

Professional English - II - HS3252 Engineering Graphics - GE3251


Statistics and Numerical Methods - Physics for Electronics Engineering -
MA3251 PH3254
Physics for Electrical Engineering - Physics for Civil Engineering - PH3201
PH3202
Materials Science - PH3251 Basic Electrical and Electronics
Engineering - BE3251
Physics for Information Science - Basic Civil and Mechanical Engineering -
PH3256 BE3255
Basic Electrical and Instrumentation Electric Circuit Analysis (Circuit
Engineering - BE3254 Theory) - EE3251
Programming in C - CS3251 Circuit Analysis - EC3251
Data Structures Design - AD3251
EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 1


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 2


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 3


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 4


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 5


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 6


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 7


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 8


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 9


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 10


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 11


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 12


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 13


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 14


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 15


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 16


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 17


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 18


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 19


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 20


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 21


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 22


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 23


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 24


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 25


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 26


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 27


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 28


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 29


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 30


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 31


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 32


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 33


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 34


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 35


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 36


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 37


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 38


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 39


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 40


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 41


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 42


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 43


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 44


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 45


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 46


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 47


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 48


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 49


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 50


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 51


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 52


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 53


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 54


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 55


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 56


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 57


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 58


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 59


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 60


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 61


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 62


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 63


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 64


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 65


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 66


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 67


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 68


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 69


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 70


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 71


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 72


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 73


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 74


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 75


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 76


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 77


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 78


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 79


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 80


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 81


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 82


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 83


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 84


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 85


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 86


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 87


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 88


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 89


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 90


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 91


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 92


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 93


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 94


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 95


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 96


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 97


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 98


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 99


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 100


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 101


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 102


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 103


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 104


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 105


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 106


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 107


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 108


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 109


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 110


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 111


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 112


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 113


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 114


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 115


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 116


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 117


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 118


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 119


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 120


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 121


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 122


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 123


EnggTree.com www.Poriyaan.in

4.1 STRUCTURE

INTRODUCTION

• C language provides a rich set of primitive and derived data types for the efficient storage and ma -
nipulation of data.

• Using C language new data types can be created. These data types are known as user-defined data
types and created by using structures, unions and enumerations.
• Arrays are used for storage of homogeneous data. They cannot be used for storage of data of dif-
ferent types.
• One of the similarities between array and structure is that both contains finite number of elements.
Thus array types and structure types are collectively known as aggregate types.

• Unions are similar to structures in all aspects except the manner in which their constituent ele -
ments are stored. In structures, separate memory is allocated to each element, while in unions, all
the elements share the same memory.
• Enumerations – for defining a data type whose objects can take a limited set of values.

NEED FOR STRUCTURE DATA TYPE / USES OF STRUCTURES

• It allows grouping together of different type of elements.


• Complex data types can be handled using nesting of structures
• Structures can be used to define records to be stored in files
• It gives flexibility to programmers to define their own data types as per the requirement.
• It is also possible to create structure pointers.

STRUCTURE
www.EnggTree.com
⮚ A structure is a collection of variables of different data types grouped under a single name.
⮚ Structures are defined as a collection of data items of different data types under a common
name. Structures are collection of related variables under one name.

Example:
Student: name, roll_no, marks

There are three aspects of working with structures.


1. Defining a structure(Creating a new type)
2. Declaring variables and constants of newly created type.
3. Using and Performing operations on objects of structure type.

STRUCTURE DEFINITION
● A structure definition consists of the keyword struct followed by an optional identifier name
known as structure tag-name and a structure declaration list enclosed within the braces.
● The structure declaration list consists of declarations of one or more variables, possibly of differ-
ent types. The variables declared inside the declaration list are known as structure members or
fields.

The general form of structure-type definition is

struct structure_name
{
type membername1;
type membername2;
…................
…................
};

Downloaded from EnggTree.com 124


EnggTree.com www.Poriyaan.in

Eg:
struct book
{
char title[25],author[25];
int pages;
float price;
};

● Structure definition can have an infinite number of members.

● After the definition of structure type, the keyword struct is used to declare its variables.
● A structure definition cannot contain an instance of itself. But it may contain a pointer to an in -
stance of itself. Such a structure is known as self-referential structure.
● A structure definition does not reserve any space in the memory.
● It is not possible to initialize the structure members during the structure definition.

Eg:
struct book
{
int pages=10; //Not valid
};

If a structure definition does not contain a structure tag-nam, then the created structured is un -
named. It is also known as anonymous structure type. The objects of anonymous type should be declared
only at the time of structure definition.

DECLARING STRUCTURE OBJECTS/VARIABLES

www.EnggTree.com
● Variables and constants of the created structure type can be declared either at the time of structure
definition or after the structure definition.

The general form of declaring structure object is

struct structure_name identifier[=initialization_list];

[=initialization_list] is optional.
(or)
struct struct_name v1,v2,....vn;
where v1,v2,..vn are variables
Eg:
struct book b1;
struct student s1,s2,stud;
A structure object declaration consist of
• The keyword struct for declaring structure variables.
• The tag name of the defined structure type.
• Comma separated list of identifiers
• A terminating semicolon.
EX :
struct book
{
char title[20]; //Defining a structure
int pages;
float price;
};
struct book b1,b2,b3; //Declaring structure variable

Downloaded from EnggTree.com 125


EnggTree.com www.Poriyaan.in

• It is also possible to combine both definition and variable declaration in one statement.

EX:

struct book
{
char title[20]; //Defining a structure
int pages;
float price;
}b1,b2,b3; //Declaring structure variable

• The objects of defined structure type cannot be declared without using the keyword struct.
• The amount of memory space allocated to it is equal to the sum of the memory space required by
all of its members.
• The structure members are assigned memory addresses in increasing order.
• The members of the structure object can be initialized by providing an initialization list. An initial -
ization list is a comma separated list of initializers.

Operations on structures
The operations that can be performed on an object of structure type can be classified into two
types.
1. Aggregate Operations
- operates on the entire operand as a whole.
2. Segregate Operations
- operates on the individual members of a structure object.

Aggregate Operations
There are four aggregate operations that can be applied on an object of a structure type.
www.EnggTree.com
1. Accessing members of an object of structure type
2. Assigning a structure object to a structure variable.
3. Address of a structure object.
4. Size of a structure.

Accessing members of an object of structure type


The members of a structure object can be accessed by
1. Direct Member Access operator (. dot operator).
2. Indirect Member Access operator (→ arrow operator).

Initialization of Structures

• The members of a structure can be initialized to constant values by enclosing the values to be as -
signed within the braces after the structure definition.
Syntax:
struct struct_name
{
member1;
member2;
.
.
.

}struct_variable={contant1, constant2,....};

(or)

struct struct_name struct_variable={contant1, constant2,….};

Downloaded from EnggTree.com 126


EnggTree.com www.Poriyaan.in

Ex:

struct date
{
int date;
int month;
int year;
}independence= {15,08,1947};
or
struct date independence={15,08,1947};

- Initializes the member variables date, month, year of independence to 15,08,1947 respectively.

Accessing Structure members.


- The members of the structures can be accessed by using the structure variable along with the
dot(.) operator.

Syntax:
variable name. member name;

Ex:
struct book
{
int id;
char name[20];
};
struct book b1;

www.EnggTree.com
For accessing the structure members from the above example.
b1.id;
b1.name; where 'b1' is the structure variable.

The structure can be defined either before main() as globally or inside main() locally.

Example program :
#include<stdio.h>
struct book //structure name
{
int id;
char name[20];
char author[15];
};
main()
{
struct book b1; // structure variable
printf(“\n Enter the book id, book name\n”);
scanf(“%d\n%s\n”,&b1.id,b1.name);
printf(“\n Book id is = %d”,b1.id); //Accessing structure member
printf(“\n Book name is = %s”,b1.name);
}

output:
Enter the book id, book name
101
Maths
Book id is = 101
Book name is = Maths

Downloaded from EnggTree.com 127


EnggTree.com www.Poriyaan.in

4.2 NESTED STRUCTURES (STRUCTURE WITHIN A STRUCTURE)

• A structure can be declared within another structure.


• Some times it is required to keep a compound data items within another compound data item is
called structure within structure or it means nesting of structures.
Syntax :

struct struct_name1
{
decl 1;
decl 2;

.…
decl n;
};
struct struct_name2
{
decl 1;
decl 2;
struct struct_name1 variable_name1; //structure within structure
.....
decl n;
};

Example Program :
#include<stdio.h>
struct date
{
www.EnggTree.com
int date, month, year;
};
struct stu_data
{
char name[20];
struct date dob;
};
main()
{
struct stu_data s ={“vinoth”,{01,03,82}};
printf(“\n Name %s”,s.name);
printf(“\n \n Date of birth : %d-%d-%d”,s.dob.date, s.dob.month, s.dob.year);
getch();
Return;
}

Output :
Name : Vinoth
Date of Birth : 01- 03- 82

Downloaded from EnggTree.com 128


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 129


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 130


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 131


EnggTree.com www.Poriyaan.in

4.4 ARRAYS OF STRUCTURES

• The C language permits to declare an array of structure variable.


• If we want to handle more records within one structure, we need not specify the number of struc-
ture variable.
• In such cases we declare an array of structure variable to store them in one structure variables.

Syntax:

struct struct_name
{
decl1;
decl2;
......
decl;
}variable_name[size];

Example :
struct marks
{
int subject1;
int subject2;
int subject3;
};
main()
{
struct marks student[3]={{95,92,89},{65,63,70},{87,76,61}};
} www.EnggTree.com
Differences between Array and Structure

Array Structure
An array is a collection of related data ele- Structure can have elements of different
ments of same type. types.
An array is derived data type structure is a user-defined one
Any array behaves like a built-in data type It must be declared and defined
An array can be increased or decreased A structure element can be added if neces-
sary.

Downloaded from EnggTree.com 132


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 133


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 134


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 135


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 136


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 137


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 138


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 139


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 140


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 141


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 142


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 143


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 144


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 145


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 146


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 147


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 148


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 149


EnggTree.com www.Poriyaan.in

4.9 UNION

 Union is a collection of variables of different data types.


 Union is also a derived data type which is used to represent dissimilar data items.
 Unions are used to create user-defined types.
 Declaration and definition of union are same as structure, but use the keyword 'union' instead of 'struct'.
 The structure and union differs in terms of storage.
 In structure, a separate memory is allocated to each member, while in unions, all the members of union
share the same memory.

Characteristics of union:
 Members of union have same memory location.
 Collection of variables of different data types.
 The keyword 'union' is used to declare a union.
 Members of the union can be accessed using the dot operator.
 Size allocated is equal to the largest data member of the union.
 Only one union member can be accessed at a time.
 The members of a union are stored in the memory in such a way that they overlap each other.

Definition and Declaration of Union


 A union definition consists of the keyword union followed by an optional identifier name and the
union declaration list enclosed within the braces.
 A union object declaration consist of
 The keyword union for declaring union variables.
 The tag name of the defined structure type.
 Comma separated list of identifiers
 A terminating semicolon.
Syntax :

union union_name
www.EnggTree.com
{
union member1;
union member2;
.....
union member n;
};
union union_name variable;

Eg :
union numbers
{
char a;
int b ;
float c;
}r;

Memory Allocation in Union:

char a
int b
float c
Address
2000 2001 2002 2004
Example program :

Downloaded from EnggTree.com 150


EnggTree.com www.Poriyaan.in

Employee details using Union.

#include<stdio.h>
#include<conio.h>
union employee
{
char name[10];
int idno;
float salary;
}e;
main()
{
printf(“Enter the name\n”);
scanf(“%s”,e.name);
printf(“Enter the id number\n”);
scanf(“%d”,&e.idno);
printf(“Enter the salary\n”);
scanf(“%f”,&e.salary);

printf(“Name : %s\n”,e.name);
printf(“Id number : %d\n”,e.idno);
printf(“Salary : %f\n”,e.salary);

getch();
return;
}

Output :

Enter the name


www.EnggTree.com
Ram
Enter the id number
101
Enter the salary
20000
Name : Ram
Id number : 101
Salary : 20000

Differences between Structure and Union

S.NO Structure Union


1 It occupies its own memory space. It uses the same space.
2 The keyword 'struct' is used. The keyword 'union' is used.
3 All members of a structure can be initialized. Only the first member of a union can be
initialized.
4 Each member is stored in a separate memory All members are stored in the same memory
locations. location.
5 More memory space is required. Less memory space is required.

Downloaded from EnggTree.com 151


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 152


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 153


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 154


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 155


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 156


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 157


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 158


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 159


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 160


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 161


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 162


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 163


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 164


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 165


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 166


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 167


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 168


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 169


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 170


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 171


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 172


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 173


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 174


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 175


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 176


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 177


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 178


EnggTree.com www.Poriyaan.in

www.EnggTree.com

Downloaded from EnggTree.com 179


Programming in C

Unit I (a): Introduction to Programming


Introduction to Computer Software | Classification of Computer Software | Programming Languages |
Generation of Programming Languages
Unit I (b): Introduction to C
Introduction, Background, Characteristics, Uses of C Programming | Structure of a C Program | Writing the First
C Program | Files Used in a C Program | Compiling and Executing C Programs | Using Comments | C Tokens |
Character Set in C | Keywords | Identifiers | Basic Data Types in C | Variables | Constants | Input/Output |
Statements in C | Operators in C | Type Conversion and Typecasting
Unit I (c): Decision Control and Looping Statements
Introduction to Decision Control Statements | Conditional Branching Statements | Iterative Statements | Nested
Loops | The Break and Continue Statements | goto Statement
Unit I (d): Preprocessor Directives
Introduction of Preprocessor Directives | Types of Preprocessor Directives | #define | #include | #undef | #line |
Pragma Directives | Conditional Directives | Defined Operator | #Error Directive | Predefined Macro Names
Unit II (a): Arrays
Introduction to Arrays in C Programming | Declaration of Array in C | Accessing the Elements of an Array in C |
Storing Values in Arrays | Operations on Arrays | Passing Arrays to Functions | Two-Dimensional Arrays |
Operations on Two-Dimensional Arrays | Passing Two-Dimensional Arrays to Functions | Multidimensional
Arrays | Sparse Matrices (Array Representation) | Applications of Arrays
Unit II (b): Strings
Introduction to Strings in C | Suppressing Input | Strings Taxonomy | Operations on Strings | Miscellaneous
String and Character Functions | Arrays of Strings
Unit III (a): Functions
Introduction to Functions | Using Functions | Function Declaration/Function prototype | Function Definition |
Function Call | Return Statement | Passing Parameters to Functions | Scope of Variables | Storage Classes |
Recursive Functions | Types of Recursion | Tower of Hanoi (recursion) | Recursion Versus Iteration
Unit III (b): Pointers
Understanding the Computer's Memory | Introduction to Pointers | Declaring Pointer Variables | Pointer
Expressions and Pointer Arithmetic | Null Pointers | Generic Pointers | Passing Arguments to Function Using
Pointers | Pointers and Arrays | Passing an Array to Functions | Difference Between Array Name and Pointer |
Pointers and Strings | Arrays of Pointers | Pointers and 2D Arrays | Pointers and 3D Arrays | Function Pointers |
Array of Function Pointers | Pointers to Pointers | Memory Allocation in C Programs | Memory Usage | Dynamic
Memory Allocation | Drawbacks of Pointers
Unit IV: Structures and Union
Structure | Nested Structures | Arrays of Structures | Structure and Functions | Self-referential Structures |
Unions | Arrays of Union Variables | Unions Inside Structures | Structures Inside Unions | Enumerated Data Type
| Memory Allocation and Deallocation for a Linked List | Singly Linked Lists
Unit V: File Processing
Introduction to Files | Using Files in C | Read Data From Files | Writing Data From Files | Detecting the End-of-
File | Error Handling During File Operations | Accepting Command Line Arguments | Function for Selecting a
Record Randomly | Remove()| Renaming the File | Creating a Temporary File
All 2nd Semester Subjects
Professional English - II - HS3252 Engineering Graphics - GE3251
Statistics and Numerical Methods - Physics for Electronics Engineering -
MA3251 PH3254
Physics for Electrical Engineering - Physics for Civil Engineering - PH3201
PH3202
Materials Science - PH3251 Basic Electrical and Electronics
Engineering - BE3251
Physics for Information Science - Basic Civil and Mechanical Engineering -
PH3256 BE3255
Basic Electrical and Instrumentation Electric Circuit Analysis (Circuit
Engineering - BE3254 Theory) - EE3251
Programming in C - CS3251 Circuit Analysis - EC3251
Data Structures Design - AD3251
Civil
CSE
Home Mech
e
EEE
ECE

2nd Semester 3rd Semester


1st Semester
Professional English II Discrete Mathematics
Professional English I
Statistics and Numerical
Methods Digital Principles and
Matrices and Calculus
Computer Organization
Engineering Graphics
Engineering Physics
Foundation of Data
Physics for Information
Science Science
Engineering Chemistry

Physics
Basic for Engineering
Electrical and Data Structure
Problem Solving and Science Engineering
Electronics
Python Programming Object Oriented
Programming in C
Programming

4th Semester 5th Semester 6th Semester


Theory of Computation Computer Networks Object Oriented Software
Engineering
Artificial Intelligence Compiler Design
and Machine Learning Embedded Systems IoT
Cryptography and
Database Management Cyber Security Open Elective I
System
Professional Elective III
Algorithms Distributed Computing
Professional Elective IV

Introduction to Operating Professional Elective I Professional Elective V


Systems
Professional Elective II Professional Elective VI
Environmental Sciences
and sustainability Mandatory Course I Mandatory Course II

7th Semester 8th Semester


Human Values and Ethics Project Work/Internship

Elective-Management

Professional Elective II

Professional Elective III

Professional Elective IV

You might also like