CS210 · Block 1 · Lesson 2

Variables and Types

Declarations · Primitive types · sizeof · Binary review · Integer overflow · Casting
At a glance: Your first real C content. You learn what goes between the curly braces: variables, types, the rules that govern how they behave in memory, and the two traps that bite cadets coming from Python. The hands-on practice happens through the cmem walkthrough in Section 5; the lab applies it all to a small aviation calculation.
Contents
  1. Lesson Objectives
  2. Assigned Readings
  3. Pre-Class Work
  4. Lesson Materials and Overview
  5. In-Class Work
  6. After Class
1. Lesson Objectives

By the end of this lesson, cadets will be able to:

  1. Declare and initialize variables of C's primitive types. (Outcome 2)
  2. Determine the size of a type using sizeof. (Outcome 2)
  3. Predict and avoid integer overflow in arithmetic operations. (Outcomes 1, 3)
  4. Apply explicit and implicit type casting in expressions. (Outcome 1)
2. Assigned Readings

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
3. Pre-Class Work
Do this first
Plan on 30 to 45 minutes for the reading and the toolchain check. Most of the learning happens during the in-class walkthrough; come ready to type.

Pre-Class Checklist

1
Read Beej-C Chapter 3
Skim if you are confident from CS110 Python. Read closely if any of these terms feel unfamiliar:
  • int, char, float, double
  • sizeof operator
  • Format specifier (%d, %f, %c, %lu)
  • Integer overflow, casting
2
Verify your toolchain still works
Open WSL, navigate to your Lesson 1 hello.c, recompile, and run.
Run:
$ cd ~/lesson01-starter
$ gcc hello.c -o hello
$ ./hello
If anything broke since Lesson 1, schedule EI before class. Setup problems will block today's lab.
3
Skim the In-Class section below
You will see an interactive cmem walkthrough widget. No need to use it yet; just know it is there and what it looks like.
4. Lesson Materials and Overview

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.

Jump to
Slides Python vs C Naming Primitive Types Format Specifiers Binary Review Integer Overflow Casting

Slides

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.

Open Lesson 2 slides →

Python vs C: the type contract

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.

Naming things in C

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.

Primitive types and their sizes

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.

Format specifiers

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
%dintprintf("%d\n", 42);
%ffloat, doubleprintf("%f\n", 3.14);
%.1ffloat, double (1 decimal)printf("%.1f\n", 3.14);
%cchar (as letter)printf("%c\n", 'A');
%lusize_t (sizeof results)printf("%lu\n", sizeof(int));

Binary review

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.

128
64
32
16
8
4
2
1
0
0
1
0
1
0
1
0
00101010 = 32 + 8 + 2 = 42

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.

Integer overflow

When you store a value larger than a type can hold, the bits wrap around. The compiler does not warn you. The classic example:

short s = 40000;
printf("%d\n", s);  // prints -25536, not 40000

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.

Casting and the division trap

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.

int a = 5 / 2;
// a is 2, not 2.5

float b = (float)5 / 2;
// b is 2.5

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.

5. In-Class Work

Four things to do in class. Use the checklist to track your progress.

  • Work through the cmem walkthrough below. Twelve guided tasks covering declarations, types, overflow, and casts. About 25 minutes.
  • Work through the binary & overflow walkthrough. Ten guided tasks that move from decimal to binary, signed vs unsigned, then watch initialization overflow happen bit by bit. About 20 minutes.
  • Take the binary review self-check. Eight quick questions covering bit positions, conversions, sizeof, signed vs unsigned, and overflow. About 10 minutes.
  • Complete Lab 02: Altitude and Temperature. Auto-graded through GitHub Classroom. Full instructions in the lab repo.

cmem Walkthrough

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.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.

CS210 Lesson 2 · cmem Memory Walkthrough

A guided tour through declarations, types, overflow, and casts. Type each C statement on the input line. The terminal shows what cmem did; the right pane shows current memory.
Task 0 of 12
Section 1: Your first declaration
Current task
Loading...
Memory
No variables yet.
Type a declaration on the left.
cmem>
How this works: This is not a Linux terminal. It is a teaching tool that simulates what C declarations do in memory. The task card tells you what to type next; only the expected command will advance the walkthrough so the memory state stays predictable. Mistype twice and the hint button will pulse. After the walkthrough completes, free mode unlocks automatically so you can experiment.

Binary & Overflow Walkthrough

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.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.

CS210 Lesson 2 · Binary & Overflow Walkthrough

Watch numbers become bits. Watch what happens when you push past a type's edge. Type the C statement the task asks for; the right pane shows the bit pattern.
Task 0 of 10
Section 1: Decimal to binary
Current task
Loading...
Binary view
No variables yet.
Type a declaration on the left.
bits>
How this works: Each declaration shows the bit grid for that value, plus how it reads as signed and as unsigned (when both make sense for the type). Sign bits (the highest bit in a signed type) are outlined in red. Watch for the overflow warning when a value does not fit in its type.

Binary Review Self-Check

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.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.

CS210 Lesson 2 · Binary Review Self-Check

Eight questions covering decimal to binary conversion, signed vs unsigned interpretation, sizeof, type ranges, and overflow. Answer each one, submit, and move forward.
How this works: One question per card. Pick or type your answer and click Submit. The feedback appears, then click Next to move on. Answers are locked once submitted, so think before you submit. When you finish all eight, you get a score and a recap.
6. After Class
  • Finish Lab 02 if you did not complete it in class. Submit through GitHub Classroom before Lesson 3.
  • Read Beej-C Chapter 3 cont. (Operators) for Lesson 3.
  • Replay either walkthrough on your own. Try different values in free mode. See if you can predict the bytes or bit pattern before you press Enter.
  • Heads up: Quiz 1 at Lesson 4 covers Lessons 2 and 3 together.
Need help? Schedule EI with your instructor. Bring specific code or specific cmem screenshots, and the exact error message or output you do not understand. Specific questions get unstuck quickly.