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

Module 5 - Structure and Union

vtu notes

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Module 5 - Structure and Union

vtu notes

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Introduction Structure
 Arrays support packing same type data together.
 Many times, packing of different types of data is necessary in
programming.
 E.g. A Student record may contain name which is string, id an integer,
cgpa, a float, etc.
 How to store and process details of 60 students in a class?? (e.g:
name,id,cgpa)
 One possible solution is:
char names[60][25];
int id[60];
float cgpa[60];
 This is not an elegant solution as processing of the data is tedious and
code size will become large.
 Structures in C: Provides a better way to deal with such situation.
 Structures enable us to create new data types of our own..!!
 Such data types are called as ‘User Defined Data types’.
 Structures can combine different built-in data types to define new data
types.

2. Defining student structure ( Creating a new data type)


For our student problem:
struct Student
{
char name[20];
int id;
float cgpa;
};
 Now Student data type behaves similar to built-in data types like int, char
etc.
3. Size of a Structure type or structure variable
 Normally, Size of a structure is the total size of the members in that
structure.
 For Student structure above, the size is 20 + 4 + 4 = 28 bytes, assuming
character size 1 byte, size of int and float are 4 bytes each.
 There are some exceptions to this. If size of structure is not a multiple of the
word size** (in bytes) of the system, some extra bytes will be added to the
size to make it a proper multiple of the word size.

4. Defining a structure
General format:
struct type-name
{
data-type1 name1;
data-type2 name2;
------------- ---------
------------- ---------
};
5. Declaring structure variables
Assume the structure is defined as:
struct mytype
{
int n;
char c;
float f;
};
 Declaring variable of type mytype.
struct mytype s1,s2;
 A structure type variable declaration is similar to any basic type
variable declaration Declaring structure variables - other ways
struct mytype
{
int n;
char c;
float f;
}s1,s2;
6. Accessing structure members
Accessing structure members & initialization. Members of a structure
variable is accesses using dot(.) operator.
Dot(.) operator is also referred as member operator. See the example.
struct mytype
{
int n;
char c;
float f;
}s1;
int main()
{
// accessing members & assigning values
s1.n = 10;
s1.c = 'a';
s1.f = 1.32;
// Displaying member values
printf("%d %c %f", s1.n, s1.c,s1.f);
return 0;
}

7. Runtime Initialization example:


Compile time initialization is similar to array initialization.
E.g. struct mytype s1 = {10,'a',2.57};

#include <stdio.h>
#include<string.h>
struct Student
{
char name[100];
char *usn;
int sem;
}
s1={"Raj","2022CS001",1};
int main()
{

struct Student s2;

strcpy(s2.name,"Steve");
s2.usn ="2022CS002";
s2.sem = 2;

printf("Student Name is: %s", s1.name);


printf("\nStudent usn is: %s", s1.usn);
printf("\nStudent sem is: %d\n", s1.sem);
printf("Student Name is: %s", s2.name);
printf("\nStudent usn is: %s", s2.usn);
printf("\nStudent sem is: %d", s2.sem);
return 0;
}
8. Run time initialization example
Reads the data directly from user during execution & stores in structure
variable.
#include<stdio.h>
#include<string.h>
int main()
{
struct employee
{
char name[20];
char job[20];
float salary;
};
struct employee e[10];
int i,n;
printf("Enter the number of Employees\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the details of Employee %d\n",i+1);
printf("Enter the Name\n");
scanf("%s",e[i].name);
printf("Enter the Job\n");
scanf("%s",e[i].job);
printf("Enter the Salary\n");
scanf("%f",&e[i].salary);
}
for(i=0;i<n;i++)
{
printf("Details of Employee %d\n",i+1);
printf("Name %s\n",e[i].name);
printf("Job %s\n",e[i].job);
printf("Salary %.2f\n",e[i].salary);
}
return 0;}
Enumerated Data type
Definition: Enumeration (or enum) is a user defined data type in C. It is mainly
used to assign names to integral constants, the names make a program easy to read
and maintain.

In the below Program ,if we do not explicitly assign values to enum names, the
compiler by default assigns values starting from 0. For example, in the following C
program, sunday gets value 0, monday gets 1, and so on.

Example-1

#include <stdio.h>

enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main()
{
// creating today variable of enum week type
enum week today;
today = Wednesday;
printf("Day %d",today+1);
return 0;}
Example-3

#include <stdio.h>

enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;

int main()
{
card = club;
printf("club value is %d",card);
return 0;
}

Example-4

#include <stdio.h>

enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday,

Friday, Saturday};

int main()

enum weekdays w; // variable declaration of weekdays type

w=Monday; // assigning value of Monday to w.

printf("The value of w is %d",w);

return 0;

}
UNION
Definition: A union is a special data type available in C that allows to store
different data types in the same memory location. You 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 memory location for multiple-
purpose.

 A union is a user-defined type similar to structs in C except for one key


difference.
 Structures allocate enough space to store all their members, whereas unions
can only hold one member value at a time.

Example-1

#include <stdio.h>
#include <string.h>

union Data {
int i;
float f;
char str[20];
};

int main( ) {

union Data data;

printf( "Memory size occupied by data : %d\n", sizeof(data));

return 0;
}

Example-2

#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;

int main() {
j.salary = 12.3;

// when j.workerNo is assigned a value,


// j.salary will no longer hold 12.3
j.workerNo = 100;

printf("Salary = %.1f\n", j.salary);


printf("Number of workers = %d", j.workerNo);
return 0;
}

Example-3

#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;

int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;}
typedef
The typedef is a keyword that is used in C programming to provide existing data
types with a new name. typedef keyword is used to redefine the name already the
existing name. When names of datatypes become difficult to use in programs,
typedef is used with user-defined datatypes, which behave similarly to defining an
alias for commands.

Example-1

#include<stdio.h>

typedef float mytype; //declared a new variable of int data type!

int main ()

int a=10;

mytype b=50,sum;

sum = a + b;

printf ("Sum is: %f", sum);

return 0;

Example-2

#include<stdio.h>
#include<string.h>

typedef struct employee


{
char name[50];
int salary;
}emp;
void main( )
{
emp e1;
printf("\nEnter Employee record:\n");
printf("\nEmployee name:\t");
scanf("%s", e1.name);
printf("\nEnter Employee salary: \t");
scanf("%d", &e1.salary);
printf("\nstudent name is %s", e1.name);
printf("\nroll is %d", e1.salary);
}

You might also like