ENG1410 CProgramming Topic10b Enum Union
ENG1410 CProgramming Topic10b Enum Union
5
Data Types
What is a Data Type?
o Defines a certain domain of values
o Defines Operations allowed on these values
Example
1. int type
o Takes only integer values
o Operations: addition, subtraction, multiplication, bitwise
operations e.t.c
2. float type
o Takes only floating point values
o Operations: addition, subtraction, multiplication, division
e.t.c (bitwise and % operations are not allowed).
6
Primitive Data Types
• Almost all programming languages provide
a set of primitive data types
• Primitive data types: Those not defined in
terms of other data types
• Some primitive data types are merely
reflections of the hardware
• Others require only a little non-hardware
support for their implementation
7
User Defined Data Types
o In contrast to primitive data types, there is a concept
of user defined data types.
o The operations and values of user defined data types
are not specified in the language itself but is specified
by the user.
o Examples of user defined data types include:
1. Typedef: C provides a capability that enables users to assign
an alternate name to a data type. This is done with a
statement known as typedef
2. Structure: by using structures, we are defining our own type
by combining other data types.
3. Enum: types are defined and used by the programmer as
need arises.
4. Union: is one of the more advanced programming applications in
which it is necessary to store different types of data in the same
storage area.
8
Typedef
Type Definition
• Syntax: typedef type Name ;
• Name becomes a name you can use for the type
• Examples:
typedef int INTEGER;
INTEGER x; /* x is an integer */
typedef char *STRING;
STRING sarray[10];
/* sarray is an array of char *’s, equivalent to declaring:
char *sarray[10]; */
10
Typedef
• Definition:– a typedef is a way of renaming a
type
• E.g.,
typedef struct motor Motor;
Motor m, n;
Motor *p, r[25];
Motor function(const Motor m; …);
11
typedef (continued)
• typedef may be used to rename any type
– Convenience in naming
– Clarifies purpose of the type
– Cleaner, more readable code
– Portability across platforms
• E.g.,
– typedef char *String;
• E.g.,
– typedef int size_t;
– typedef long int32;
– typedef long long int64;
12
“enum” Data Type
Enum Data Types
enum is a user-defined type consisting of a set of named
constants called enumerators.
Using a keyword enum, it is a set of integer constants
represented by identifiers.
The syntax is shown below, ([is optional])
// for definition of enumerated type
enum [tag]
{
enum-list
}
[declarator];
And
// for declaration of variable of type tag
enum tag declarator;
The keyword enum along with the tag name day are used as
a type specifier to declare variables of type enum day.
16
Alternative var Declaration
On the previous slide, the enumeration type
enum day was first declared, then variables of
that type were declared.
o We can do both at the same time.
enum day {sun,mon, tue, wed, thu, fri, sat} d1, d2;
17
Enum Data Types: What Are they?
enum day {sun, mon, tue, wed, thu, fri, sat} d1;
18
Enum Data Types: Initialization
19
Enum Data Types: Example
// An example program to demonstrate working
// of enum in C
#include<stdio.h>
20
Enum Data Types: Example
// Another example program to demonstrate
// working of enum in C
#include<stdio.h>
return 0;
}
21
“union” Data Type
Union Data Types
One of the more unusual constructs in the C programming
language is the union.
This construct is used in applications in which it is necessary
to store different types of data in the same storage area.
For example if you want to define a single variable called x,
which could be used to store (a) a single character, (b) a
floating point member, or (c) an integer, you could define a
union say called mixed as follows:
24
Union Data Types
The storage associated with a union variable is
the storage required for the largest member of
the union.
When a smaller member is stored, the union
variable can contain unused memory space.
All members are stored in the same memory
space and start at the same address.
Hence, the stored value is overwritten each time
a value is assigned to a different member.
Begin the declaration of a union with the union
keyword, and enclose the member list in curly
braces.
25
Union Data Types
Union are particularly useful in embedded programming or in situations
where direct access to the hardware/memory is needed depending on
the endianism and processor architecture.
For example, union can be used to breakdown hardware registers into
the component bits. So, you can access an 8-bit register bit-by-bit.
Unions are used when you want to model struct defined by hardware,
devices or network protocols, or when you are creating a large number
of objects and want to save space.
In most occasion of the program running, you really don't need them
90% of the time. (just an assumption!)
Most of the cases the union is wrapped in a struct and one member
of the struct tells which element in the union to access.
In effect, a union is a structure in which all members have offset zero
from the base.
The same operations are permitted on union as on struct:
assignment to or copying as a unit, taking the address, and accessing a
member.
26
union Data Types: Example
#include <stdio.h>
#include <string.h>
union Record {
int i;
float f;
char str[20];
};
int main( ) {
The system will print:
union Record record; // var declaration
record.i : 1917853763
record.f : 4122360580327794
record.i = 10;
record.str : C Programming
record.f = 220.5;
strcpy( record.str, "C Programming");
return 0;
}
27
union Data Types: Example
#include <stdio.h>
#include <string.h>
union Record {
int i;
float f;
char str[20];
};
int main( ) {
return 0;
}
28
Summary
Summary
o User Defined Data Types are a powerful constructs
to build more complex programs.
o User Defined Data Types in “C” can be any of the
following types:
o enum: is used to assign names to the integral constants
which makes a program easy to read and maintain.
o struct: structures are aggregate data types. They are useful
for many applications such as databases, banking and
many others.
o union: Unitions allow you to create variables which share a
common memory space. Unions are a bit like structures, in
that they appear to be aggregate data types. However, the
main difference is that the union members occupy
overlapping memory areas.
typedef: Programmers typically use typedef keyword to
improve the readability of the code and as a means of
abbreviating it.
30
Resources
Union/Enum: Resources
o YouTube:
https://www.youtube.com/watch?v=9QdJExC2AVg
https://www.youtube.com/watch?v=oySsPUDr35U
o Documents:
https://docs.microsoft.com/en-us/cpp/c-language/c-enumeration-
declarations?view=msvc-160
https://www.tutorialspoint.com/cprogramming/c_unions.htm
o Examples:
https://www.programiz.com/c-programming/c-enumeration
https://www.geeksforgeeks.org/enumeration-enum-c/
https://www.programiz.com/c-programming/c-unions
o Test Yourself:
https://www.prep.youth4work.com/practice-_test_s/c-programming-
test/structures__comma__-unions-and-enumerations-test
32