ECEC 201 · Week 2

Modern Computer: Von Neumann

A C type is a size in memory, and the size belongs to the system, not the language.

Week 2 of 10 · Das Frite

ECEC 201 · Week 2 · In class

A C type is a size in memory, and the size belongs to the system, not the language.

ECEC 201 · Week 2 · In class

Signed and unsigned values wrap differently at their limits.

ECEC 201 · Week 2 · Lecture

A type is a size the system chooses

ECEC 201 · Week 2 · Lecture

Printing the right way

#include <stdio.h>

int main(void)
{
	printf("short: %zu bytes\n", sizeof(short));
	return 0;
}
ValueFormat
sizeof result (size_t)%zu
int / unsigned%d / %u, hex %x
long long%lld

ECEC 201 · Week 2 · Lecture

Two's complement, in 8 bits

BitsHexAs unsignedAs signed
0111 11110x7F127127
1000 00000x80128-128
1111 11110xFF255-1

Same bits, two readings. The top bit is the sign in the signed reading, so one step past the largest positive pattern lands on the most negative one.

ECEC 201 · Week 2 · Lecture

Defined wrap, undefined overflow

ECEC 201 · Week 2 · Lab

Lab

  1. Write a program that prints `sizeof` for char, short, int, long, long long, float, double, and a pointer.
  2. Add one to a signed and to an unsigned 32-bit value at their limits, and print both results in decimal and hexadecimal. Second pass: compile with `-fsanitize=undefined`, run again, and record the line it reports.

ECEC 201 · Week 2 · Lab

Lab (continued)

  1. Assemble the lab hand-in: The program, its `sizeof` table, the two overflow results, and the sanitizer line.

ECEC 201 · Week 2 · Lab

Hand in

The program, its `sizeof` table, the two overflow results, and the sanitizer line.

ECEC 201 · Week 2 · Homework

Homework

ECEC 201 · Week 2 · Homework

Turn in

The predictions, marked where the board disagreed.

ECEC 201 · Week 2 · Read

Read

On the lab site: Module 1, What a board is, names the SoC. This week grades C type sizes measured on that SoC.

ECEC 201 · Week 2 · Next

Week 3

A pointer is an address.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.