You are on page 1of 43

Programming Concept

Chapter 9: C Structures, Unions, Bit


Manipulation, and Enumerations

Heri Prasetyo, S. Kom, M. Sc. Eng, Ph. D.


Department of Informatics
University of Sebelas Maret (UNS)
Surakarta, Indonesia
University of Sebelas Maret (UNS)

Introduction
• Structures—sometimes referred to as aggregates—are
collections of related variables under one name.

• Structures may contain variables of many different data


types—in contrast to arrays, which contain only elements
of the same data type.

• Structures are commonly used to define records to be


stored in files (see Chapter 11, C File Processing).

• Pointers and structures facilitate the formation of more


complex data structures such as linked lists, queues,
stacks and tree.
Programming Concept 2
University of Sebelas Maret (UNS)

Introduction
• typedefs—for creating aliases for previously defined data types.

• unions—derived data types like structures, but with members that


share the same storage space.

• bitwise operators—for manipulating the bits of integral operands.

• bit fields—unsigned int or int members of structures or unions for


which you specify the number of bits in which the members are
stored, helping you pack information tightly.

• enumerations—sets of integer constants represented by identifiers.

Programming Concept 3
University of Sebelas Maret (UNS)

Structure Definition

Programming Concept 4
University of Sebelas Maret (UNS)

Self-Referential Structures

Programming Concept 5
University of Sebelas Maret (UNS)

Defining Variables of Structure


Types

Programming Concept 6
University of Sebelas Maret (UNS)

Operations That Can Be Performed on


Structures
The only valid operations that may be performed on
structures are:
• assigning structure variables to structure variables of
the same type.
• taking the address (&) of a structure variable.
• accessing the members of a structure variable.
• using the sizeof operator to determine the size of a
structure variable.

Programming Concept 7
University of Sebelas Maret (UNS)

Example

Programming Concept 8
University of Sebelas Maret (UNS)

Initializing Structures

Programming Concept 9
University of Sebelas Maret (UNS)

Example

Programming Concept 10
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 11
University of Sebelas Maret (UNS)

typedef

Programming Concept 12
University of Sebelas Maret (UNS)

Example

Programming Concept 13
University of Sebelas Maret (UNS)

Programming Concept 14
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 15
University of Sebelas Maret (UNS)

Unions
• A union is a derived data type—like a structure—
with members that share the same storage space.

• For different situations in a program, some variables


may not be relevant, but other variables are—so a
union shares the space instead of wasting storage on
variables that are not being used.

• The members of a union can be of any data type. The


number of bytes used to store a union must be at least
enough to hold the largest member.
Programming Concept 16
University of Sebelas Maret (UNS)

Unions Declaration

Programming Concept 17
University of Sebelas Maret (UNS)

Example

Programming Concept 18
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 19
University of Sebelas Maret (UNS)

Bitwise Operators
• The bitwise operators are bitwise AND (&), bitwise
inclusive OR (|), bitwise exclusive OR (^; also
known as bitwise XOR), left shift (<<), right shift
(>>) and complement (~).

Programming Concept 20
University of Sebelas Maret (UNS)

Bitwise Operators

Programming Concept 21
University of Sebelas Maret (UNS)

Displaying an Unsigned Integer in Bits

Programming Concept 22
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 23
University of Sebelas Maret (UNS)

Making Function displayBits


More Scalable and Portable

Programming Concept 24
University of Sebelas Maret (UNS)

Using the Bitwise AND, Inclusive OR, Exclusive


OR and Complement Operators

Programming Concept 25
University of Sebelas Maret (UNS)

Programming Concept 26
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 27
University of Sebelas Maret (UNS)

Results of combining two bits with the bitwise


inclusive OR operator |.

Programming Concept 28
University of Sebelas Maret (UNS)

Results of combining two bits with the bitwise


exclusive OR operator ^.

Programming Concept 29
University of Sebelas Maret (UNS)

Using the Bitwise Left- and Right-Shift


Operators

Programming Concept 30
University of Sebelas Maret (UNS)

Programming Concept 31
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 32
University of Sebelas Maret (UNS)

Bitwise Assignment Operators

Programming Concept 33
University of Sebelas Maret (UNS)

Operator precedence and associativity

Programming Concept 34
University of Sebelas Maret (UNS)

Bit Fields

Programming Concept 35
University of Sebelas Maret (UNS)

Representing Card with Bit Fields

Programming Concept 36
University of Sebelas Maret (UNS)

Programming Concept 37
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 38
University of Sebelas Maret (UNS)

Enumeration Constants

Programming Concept 39
University of Sebelas Maret (UNS)

Using Enumerations

Programming Concept 40
University of Sebelas Maret (UNS)

Execution Result

Programming Concept 41
University of Sebelas Maret (UNS)

Thank you….

Programming Concept 42
University of Sebelas Maret (UNS)

Q&A

Programming Concept 43

You might also like