By the end of this lesson, cadets will be able to:
sizeof. (Outcome 2)Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 3 (Variables and Statements) | Declarations, types, and basic statements |
| King, C Programming: A Modern Approach | Chapter 7 (Basic Types) | Optional deeper dive into sizes and ranges |
int, char, float, doublesizeof operator%d, %f, %c, %lu)Today is the first lesson where C diverges from Python in ways that bite you. The setup matters: Python and C both have variables, but C variables are a different beast. The reference material below covers the concepts you will see in lecture and use in the cmem walkthrough.
The Lesson 2 deck covers declarations, types, sizeof, the overflow trap, the division trap, and casts. The slides are the spine of the in-class lecture; the cmem walkthrough is where you practice each concept hands-on.
In Python you wrote x = 5 and the interpreter figured out the type for you. In C you write int x = 5; and you and the compiler enter into a contract. You promise the variable is an int; the compiler reserves exactly 4 bytes and interprets those bits as an integer forever after.
The trade is real. In return for the discipline, C runs faster, uses less memory, and lets you reason about exactly what is happening at the machine level. That precision is also what makes the overflow trap and the division trap possible. The compiler trusts you; if you lie to it about the value, it stores the wrong bits without complaint.
C uses snake_case for variables and functions and SCREAMING_SNAKE_CASE for constants and macros. Variables get descriptive names, one per line, initialized on declaration. The DFCS C Programming Standard formalizes this in §5.1 (variable naming) and §5.4 / §5.6 (constants and macros). The full standard lives in the course Resources section; you will see it referenced again whenever a new style rule becomes teachable.
Concretely: int altitude_ft = 15000; not int a = 15000; and not int altitudeFt = 15000;. This is how the C standard library, POSIX, and the Linux kernel write code; CS210 expects the same.
These are the sizes on the cs210 lab machines (64-bit Linux, gcc). The C standard only guarantees minimum sizes; on other platforms these can differ.
| Type | Size | Range or notes |
|---|---|---|
| char | 1 byte | Single character. Stored as a small integer (ASCII code). |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| int | 4 bytes | About ±2.1 billion. Your default whole-number type. |
| long | 8 bytes | About ±9.2 quintillion |
| float | 4 bytes | IEEE 754 single precision. ~7 decimal digits of precision. |
| double | 8 bytes | IEEE 754 double precision. ~15 decimal digits. |
When you print a value with printf, you tell it how to interpret the bits with a format specifier. The wrong specifier prints garbage. CS210 builds with -Wall -Werror, so a format-specifier mismatch is a build failure, not a warning. Read the message and fix the specifier.
| Specifier | For type | Example |
|---|---|---|
| %d | int | printf("%d\n", 42); |
| %f | float, double | printf("%f\n", 3.14); |
| %.1f | float, double (1 decimal) | printf("%.1f\n", 3.14); |
| %c | char (as letter) | printf("%c\n", 'A'); |
| %lu | size_t (sizeof results) | printf("%lu\n", sizeof(int)); |
You saw binary in CS110. Here is a quick refresher because today’s overflow trap and the binary walkthrough in Section 5 both rely on it. If this all feels familiar, skip ahead; if it feels rusty, take three minutes and read.
Bits and bit positions. A bit is a single 0 or 1. Each position in a binary number represents a power of 2, starting from the right at 20 = 1.
Decimal to binary. To convert a decimal number to binary, find the largest power of 2 that fits, subtract it, repeat. For 42: 32 fits (42 - 32 = 10), then 8 fits (10 - 8 = 2), then 2 fits (2 - 2 = 0). Bits set at positions 5, 3, and 1, so 00101010.
Binary to decimal. Add up the position values of the bits that are set. 01110 has bits set at positions 3, 2, and 1: 8 + 4 + 2 = 14.
Bytes and bits. 1 byte = 8 bits. So a 4-byte int is 32 bits, a 2-byte short is 16 bits, and an 8-byte double is 64 bits. sizeof reports bytes; multiply by 8 to get bits.
Signed vs unsigned. The same bit pattern means different things depending on the type. unsigned short reads all sixteen bits as a positive number (0 to 65,535). short uses two’s complement: the top bit is the sign (0 positive, 1 negative), and the range is -32,768 to 32,767. So the bit pattern 1111111111111111 is 65,535 as unsigned and -1 as signed. The bits do not change; the type tells the compiler how to interpret them.
All of this becomes hands-on in the binary walkthrough in Section 5. You can also test yourself with the self-check quiz before class.
When you store a value larger than a type can hold, the bits wrap around. The compiler does not warn you. The classic example:
A short can hold -32,768 to 32,767. You asked it to hold 40,000; the bit pattern that gets stored is interpreted as -25,536. The cmem walkthrough in Section 5 lets you watch this happen.
Two ways types convert in C: implicit (the compiler does it automatically when types differ) and explicit (you write (type) in front of a value). The most common reason to cast explicitly is to fix the integer division trap.
Rule of thumb: if both operands of / are integer types, C performs integer division and truncates the decimal. To get a fractional result, at least one operand must be float or double, which usually means casting one of them.
Four things to do in class. Use the checklist to track your progress.
Twelve tasks across four sections. Type each C statement at the prompt. The terminal shows what cmem did; the right pane shows current memory state.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.
Ten tasks across four sections. See what numbers actually look like in binary, why signed and unsigned interpret the same bits differently, and what happens when you store a value that does not fit in its type.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.
Eight questions. Most are quick. Answer each one and click Check to see how you did. The final summary tells you whether you are ready to move on or whether you should revisit the binary refresher first.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.