You are on page 1of 35

Chapter 3

Getting the Most out of C

1
INTEGER DATA TYPES

2
INTEGER DATA TYPES

3
INTEGER DATA TYPES

4
BOOLEAN DATA TYPES

5
MIXING DATA TYPES

 The condition in the if statement above may at first appear to always be true .
 In fact, however, the condition is always false

6
MIXING DATA TYPES

 Conversions are considered to be either.


 Promotion (assigning an integer value to a floating-point variable)
 Demotion (assigning floating-point value to an integer variable, or a 32-bit integer value to an 8-bit
char)
 Conversions may be.
 Implicitly (double gpa = 4 ;)
 Explicitly (double gpa = (double) 4 ;)

7
MANIPULATING BITS IN MEMORY

 Small embedded systems also pack data.

 Manipulation of these bits is implemented using C’s bitwise operators.

8
MANIPULATING BITS IN MEMORY

9
MANIPULATING BITS IN MEMORY

10
Testing Bits

11
Setting, Clearing, and Inverting Bits

12
Setting, Clearing, and Inverting Bits

13
Extracting Bits

14
Inserting Bits

15
MANIPULATING BITS IN INPUT/OUTPUT PORTS

16
Write-Only I/O Devices

17
I/O Devices Differentiated by Reads Versus Writes

18
I/O Devices Differentiated by Sequential Access

19
I/O Devices Differentiated by Sequential Access

20
I/O Devices Differentiated by Bits in the Written Data

21
ACCESSING MEMORY-MAPPED I/O DEVICES

 Some of the unused memory addresses can be assigned to I/O devices.


 Data written to a specific memory address is sent as a command to a particular I/O device
 Data read from that same address might provide the current status of the device.
 Example.
 The color-display buffer is mapped to memory starting at address
 Each character position is assigned two bytes

22
Accessing Data Using a Pointer

23
Arrays, Pointers, and the “Address of” Operator

24
Arrays, Pointers, and the “Address of” Operator

 We can access our display buffer as an array by initializing a pointer to the base
address of the display buffer and then applying subscripting:

25
Arrays, Pointers, and the “Address of” Operator

 It would be nicer to use one subscript for row, a second for column, and a third to
differentiate between the ASCII and attribute bytes of the character cell.

26
STRUCTURES

 a structure is an aggregate data type composed of several distinct members.

27
STRUCTURES

 Structure members may be used like regular variables.

28
Packed Structures

 The compiler may align structure members on word boundaries for faster access
time.

29
Packed Structures

30
Packed Structures

31
Bit Fields

32
Bit Fields

33
VARIANT ACCESS

 Casting the address of an object.

34
Using Unions

35

You might also like