module 3
module 3
1. With proper format and example explain typedef and enumerated type.
Typedef:
A type definition (typedef), gives a name to a data type by creating a new data type
that can then be used anywhere a type is permitted.
Ex: char*stringPtrAry[20];
We can simplify the declaration by using the type definition to create string type and
then defining the array using the new type as below:
typedef char*STRING;
STRING StringPtrArt[20];
The structure defined using the keyword typedef is called type defind structure.
Syntax:
typedef struct
datatype member1;
datatype member2;
---------------
} tagName;
Ex:
typedef struct
char name[20];
float salary;
int empno;
} employee;
Enumerated type:
The enumerated type is a user defined type based on the standard integer type. In an
enumerated type, each integer value is given an identifier called an enumeration
constant.
To declare an enumerated type, we must declare its identifier and its values. Because
it is derived from the integer type, it's operations are the same as for integers.
Syntax:
enum-keyword
Identifier list (uppercase)-these are the constant values that belong to enum.
If we do not explicitly assign the values, the compiler assigned the first identifier as
the value 0 and second identifier as 1 and so on and incremented by 1
The color type has four and only four possible values. The range of values is 0---3
with the identifier,
green as 2,
and white as 3.
Once we have declared an enumerated type, we can create variables from it just as
we can create variables from standard types.
2. enumcolor skyColour;
Syntax:
struct tagName
datatype member1;
datatype member2;
------------
------------
datatype membern;
};
Example :
struct employee
{
char name[20];
float salary;
int empno;
};
4. A terminating semicolon.
• The declaration
struct book_bank
char title[20];
char author[15];
int pages;
float price;
2.Usage: Structures are used to represent complex data types with multiple fields
• while arrays are used to store multiple elements of the same data type.
4.Size: The size of a structure is determined by the sum of the sizes of its individual
fields
• whereas arrays are initialized by providing a list of values enclosed in curly braces.
7.Flexibility: Structures provide flexibility in terms of the types and numbers of fields
they can have
• while arrays have a fixed size and type for all elements.
10. Syntax
• Structure:
Struct structure_name {
Data_type member1;
Data_type member2;
// and so on…
};
• Array:
Data_type array_name[size];
11.Example:
// Structure Example
Int age;
Float height; };
// Array Example
typedef struct
int numerator;
int denominator;
}Fraction;
int main()
Fraction fr1;
Fraction fr2;
Fraction fr3;
scanf(“%d/%d”,&fr1.numerator,&fr2.numerator);
scanf(“%d/%d”,&fr1.denominator,&fr2.denominator);
res.numerator=fr1.numerator*fr2numerator;
res.denominator=fr1.denominator*fr2denominator;
return 0;
OUTPUT 1:
OUTPUT 2:
b.Indirection(using a pointer):
This involves creating a temporary pointer to the structure and then using
the * operator to access the members of the structure.
Syntax:
(*ptr).member_name
PROGRAM:
#include <stdio.h>
typedef struct
{
char name[10];
int sem;
}student;
void main()
student s = {"Sanketh",2};
student *ptr;
ptr = &s;
printf("Name : %s\n",(*ptr).name);
printf("Sem : %d\n",(*ptr).sem);
Output:
Referred by pointer is
Name : Sanketh
Sem : 2
This is used when there is a pointer to a structure. It’s denoted by dash followed by a
greater than sign(->). It dereferences the pointer and then accesses the member of
the structure.
Syntax:
ptr->member_name
PROGRAM:
#include <stdio.h>
typedef struct
{
char name[10];
int sem;
}student;
void main()
student s = {"Tejas",2};
student *ptr;
ptr = &s;
printf("Name : %s\n",ptr->name);
printf("Sem : %d\n",ptr->sem);
Output:
Name : Tejas
Sem : 2
Syntax:
struct name_1
{
member 1;
member 2;
.
.
member n;
struct name_2
{
member_1;
member_2; Inner Structure
…
member n;
} var1
} var2
Example:
struct person
char name;
int age;
float height;
struct date
int day;
int month;
int year;
} d;
} p;
Here, the inner structure date contains a variable named d with three members.
Which are day, month and year,where as the outer structure person contains
variable of name p with members as name, age, height and a structure date.
The member of a nested structure can be accessed using the following syntax:
Syntax:
p.name
p.height
p.d.day
p.d.year
Example:
OUTPUT
Name = John
Age = 30
Keyword A user can deploy the A user can deploy the keyword union to
keyword struct to define a define a Union.
Structure.
Accessing A user can access individual A user can access only one member at a given
Members members at a given time. time.
{ type element_1;
type element_2; .
. .
. } variable_1, variable_2, …;
} variable_1, variable_2, …;
Size A Structure does not have a A Union does not have a separate location for
shared location for all of its every member in it. It makes its size equal to
members. It makes the size of a the size of the largest member among all the
Structure to be greater than or data members.
equal to the sum of the size of
its data members.
Value Altering Altering the values of a single When you alter the values of a single
member does not affect the member, it affects the values of other
other members of a Structure. members.
Storage of Value In the case of a Structure, there In the case of a Union, there is an allocation
is a specific memory location of only one shared memory for all the input
for every input data member. data members. Thus, it stores one value at a
Thus, it can store multiple time for all of its members.
values of the various members.
Initialization In the case of a Structure, a In the case of a Union, a user can only initiate
user can initialize multiple the first member at a time.
members at the same time.
#include <stdio.h>
struct Rectangle {
float length;
float width;
};
int main() {
struct Rectangle r;
scanf("%f", &r.length);
scanf("%f", &r.width);
return 0;
Output 1:
Output 2:
typedef struct
int numerator;
int denominator;
}Fraction;
Fraction getFr(void);
int main(void)
Fraction fr1;
Fraction fr2;
Fraction res;
fr1 = getFr();
fr2 = getFr();
printFr(fr1,fr2,res);
return 0;
Fraction getFr(void)
Fraction fr;
scanf("%d/%d",&fr.numerator,&fr.denominator);
return fr;
Fraction res;
res.numerator=fr1.numerator*fr2.numerator;
res.denominator=fr1.denominator*fr2.denominator;
return res;
return;
Output 1
Output 2
Program explanation:
Q10 With C program demonstrate how function can access the members
of a structure in three ways.
#include <stdio.h>
typedef struct
char name[100];
int jersey;
}PLAYER;
void main()
PLAYER *ptr;
ptr = &p2;
OUTPUT:
THERE IS NO Q11
Q12 With an example illustrate operations on structure.
The structure is an entity that can be treated as a whole.However, only one
operation,assignment is allowed on the structure itself.That is, a structure can only
be copied to another structure of the same type using the assignment operator.
Rather than assign individual members when we want to copy one structure to
another,as we did earlier,we can simply assign one to the other.
Eg:
Ans The union is a construct that allows memory to be shared by different type of
data. This redefinition can be a simple as redeclaring an integer as four characters or
as complex as redeclaring an entire structure.
The union follows the same format syntax as the structure .In fact with the exception
of the keyboards struct and union the formats are the same
SYNTAX
Union shareData
{
Char chAry[2];
Short num;
};
Out put
The value stored in member1=15
Q14. Write a C program that uses a function that accepts the structure
representing a point (a point in a plane can be represented by its two
coordinates, x and y) and returns an integer 1, 2, 3, or 4 that indicates in
which quadrant the point is located.
Logic: The program defines a structure Point to represent a point in a plane with two
integer coordinates x and y. The function getQuadrant takes a Point structure as
input and returns an integer representing the quadrant in which the point is located.
It uses conditional statements to determine the quadrant based on the signs of the
coordinates. If x-coordinate and y-coordinate values of the Point are positive, then
the point is in first quadrant. If x-coordinate value is negative and y-coordinate value
is positive, then the point is in second quadrant. If x-coordinate and y-coordinate
values of the Point are negative, then the point is in third quadrant. If x-coordinate
value is positive and y-coordinate value is negative, then the point is in fourth
quadrant.
In the main function, the user inputs the coordinates of the point. The program then
calls the getQuadrant function to determine the quadrant of the point and outputs
the result accordingly.
C Program
#include <stdio.h>
struct Point
int x; int y;
};
{
if (p.x > 0 && p.y > 0) {
else
} int main()
struct Point p;
scanf("%d", &p.x);
scanf("%d", &p.y);
switch (quadrant)
break;
break;
break;
break;
return 0;