C Programming Overlays Nonotes
C Programming Overlays Nonotes
1
Table of Contents
Introduction
Undefined behaviour
Integer representations
2
Table of Contents
Introduction
Undefined behaviour
Integer representations
3
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
The C programming language
4
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Programming for hardware
5
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to assembly code
6
Comparing C to C++
7
Comparing C to C++
7
Comparing C to C++
7
Comparing C to C++
7
Comparing C to C++
7
Comparing C to C++
7
Table of Contents
Introduction
Undefined behaviour
Integer representations
8
Syntax and semantics
9
Syntax and semantics
9
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Implementation-defined behaviour
10
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Undefined behaviour
11
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
Compilers can find some of these problems, but for weird reasons, those
warnings are often switched off by default! Make sure you enable -Wall
-Wextra when you compile code.
12
Examples of undefined behaviour
12
Table of Contents
Introduction
Undefined behaviour
Integer representations
13
Values
14
Values
14
Values
14
Values
14
Values
14
Values
14
Values
14
Values
14
Addresses
• You can get the address of a variable using the & operator:
int a; &a
15
Addresses
• You can get the address of a variable using the & operator:
int a; &a
• You then obtain a pointer to a
15
Addresses
• You can get the address of a variable using the & operator:
int a; &a
• You then obtain a pointer to a
• A pointer to a type is denoted as type*, e.g. int*, char*.
15
Addresses
• You can get the address of a variable using the & operator:
int a; &a
• You then obtain a pointer to a
• A pointer to a type is denoted as type*, e.g. int*, char*.
15
Addresses
• You can get the address of a variable using the & operator:
int a; &a
• You then obtain a pointer to a
• A pointer to a type is denoted as type*, e.g. int*, char*.
We will return to pointers later
15
Types
16
char
17
char
17
char
17
char
17
char
17
char
17
char
17
Tricky char
18
Tricky char
18
Tricky char
18
Tricky char
18
Tricky char
18
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Integral types
19
Other integer types
20
Other integer types
20
Other integer types
20
Better integer types
• All those varying byte sizes of int et al. make it hard to write
efficient portable code
• Solution: use fixed-size integer types defined by stdint.h
– uint8_t is an 8-bit unsigned integer
– int8_t is an 8-bit signed integer
– uint16_t is a 16-bit unsigned integer
– ...
– int64_t is a 64-bit signed integer
21
Floating-point and complex values
22
Floating-point and complex values
22
Floating-point and complex values
22
Floating-point and complex values
22
Floating-point and complex values
22
Floating-point and complex values
23
Implicit type conversion
24
Implicit type conversion
24
Implicit type conversion
24
Explicit casts
25
Explicit casts
25
Explicit casts
25
Explicit casts
25
A small quiz
26
A small quiz
26
Table of Contents
Introduction
Undefined behaviour
Integer representations
27
Two’s complement
28
Two’s complement
28
Two’s complement
28
Two’s complement
28
Two’s complement
28
Two’s complement
28
Endianess
29
Endianess
29
Endianess
29
Endianess, let’s try again
P3
• Take 4-byte integer a = i=0 ai 2
8i
30
Endianess, let’s try again
P3
• Take 4-byte integer a = i=0 ai 2
8i
30
Endianess, let’s try again
P3
• Take 4-byte integer a = i=0 ai 2
8i
30
Endianess, the conclusion
31
Endianess, the conclusion
31
Endianess, the conclusion
31
Endianess, the conclusion
31
Endianess, the conclusion
31
Endianess, the conclusion
31
Memory addresses
32
Memory addresses
32
Memory addresses
32
Memory addresses
32
Back to pointers
33
Back to pointers
33
Back to pointers
33
Heap addresses
34
Heap addresses
The value a = 4
The addr &a = 0x55b899d552a0
34
Heap addresses
The value a = 4
The addr &a = 0x55b899d552a0
34
Heap addresses
The value a = 4
The addr &a = 0x55b899d552a0
34