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

Lec 15- Programming Fundamentals (1) (3)

Uploaded by

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

Lec 15- Programming Fundamentals (1) (3)

Uploaded by

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

CSC113-Programming Fundaments

Lecture 12
Week 15
Ms. Noor-ul-Huda
Lecturer
Department of Computer Science
College of Computer Science and Information Systems
noor.huda@iobm.edu.pk
Lecture Objectives:
•Structures
•Unions
Structures
• A structure is a collection of variables of different data types grouped
together under a single name.
• Each variable in a structure is assigned its own memory location,
allowing them to hold independent values.

2/25
Structures
• The struct keyword is used to define the structure in the C programming
language.
• The items in the structure are called its member and they can be of any
valid data type.

2/25
Syntax of Structures
• struct structure_name {

• data_type member_name1;
• data_type member_name1;
• ....
• ....
• };
2/25
Intitialize Structure members
• We can initialize structure members in 3 ways which are as follows:

• Using Assignment Operator.

• Using Initializer List.

• Using Designated Initializer List.

2/25
Intitialize Structure members
• Example of a structure called Employee that stores information about an
employee:
• struct Employee {

• int id;

• char name[50];

• float salary; };

2/25
Structure
• struct Employee emp;

• emp.id = 1;

• strcpy(emp.name, "John Doe");

• emp.salary = 50000.00;

• printf("Employee ID: %d\n", emp.id);

• printf("Employee Name: %s\n", emp.name);

• printf("Employee Salary: %.2f\n", emp.salary); 2/25


Union
• A union is similar to a structure, but all its members share the same
memory location. This means that only one member can have a value at a
time.
• Imagine a union as a single box with only one slot. You can put different
things in the box, but only one thing can be there at a time.

2/25
Union
• union Data {

• int number;

• float decimal;

• };

• the Data union has two members:

• number: An integer to store an integer value.

• decimal: A float to store a decimal value. 2/25


Union
union Data value;

value.number = 10; // only number will have a value

printf("Number: %d\n", value.number); // prints 10

value.decimal = 3.14; // decimal will now have a value, and number will lose its value

printf("Decimal: %.2f\n", value.decimal); // prints 3.14

printf("Number: %d\n", value.number); // undefined behavior, as number lost its value when
decimal was assigned 2/25
Key Differences
• Memory allocation: Structures allocate separate memory for each member, while unions share the
same memory for all members.

• Data storage: Structures can hold independent values for each member, while unions can only hold
one value at a time for any of its members.

• Use cases: Structures are used to group related data that needs to be accessed independently, while
unions are used to save memory when only one member needs to be active at a time.

2/25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy