Types, Control Flow, and Functions Workshop
This practice page exercises Clusters 2 and 3. Finish the matching concept pages before starting.
Use this compile command for every program below:
gcc -Wall -Wextra -std=c11 -o prog prog.c
Promote to -Werror once the program is clean.
Retrieval Prompts
- Define integer promotion. Which types does it apply to?
- State one precedence trap and one sequence-point rule from memory.
- State what
staticdoes at file scope, and separately what it does at block scope. - State the difference between a function declaration and a function definition.
- Name three preprocessor hazards and a rule that avoids each.
Compare and Distinguish
int,long,long long,size_t,int32_t: which to use when and why- unsigned overflow vs signed overflow
staticat file scope vs block scopeextern int x;in a header vsint x = 0;in a.cfile- macro-like function vs
static inlinefunction
Common Mistake Check
For each, identify the error:
if (x = 5) { ... }#define SQUARE(x) x*xprintf("%d\n", sizeof(int));int arr[10]; void f(int arr[10]) { size_t n = sizeof arr / sizeof arr[0]; ... }register int *p = &x;for (unsigned i = n; i >= 0; i--) { ... }int *arr = (int*)malloc(sizeof(int)); for (i = 0; i < 10; i++) arr[i] = i;#define MAX 100;used asfor (int i = 0; i < MAX; i++)
Precedence and Sequence Points Drill
For each expression, give the value on a platform where int is 32-bit, or mark UB. Then parenthesize it the way it actually parses.
1 + 2 * 31 << 2 + 3a < b ? 1 : 2 + 3!0 == 10x01 | 0x02 & 0x03- Given
int i = 0;, the value ofi++ + ++i - Given
int i = 0; int a[3] = {10, 20, 30};, the value aftera[i] = i++; - Given
int i = 1;, the value of(i++, i++, i)
Integer Width Drill
Write a program that prints, on your platform:
printf("%zu %zu %zu %zu %zu\n",
sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(long long));
Explain any differences between Linux (LP64) and Windows (LLP64).
Control Flow Drill
Write each as a small program:
- A
switchon acharthat classifies whitespace, digit, vowel, consonant, other. Use fall-through deliberately with/* fall through */comments, andbreakelsewhere. - A
forloop that prints only FizzBuzz numbers 1..100 usingcontinuefor the non-printing cases. - A
do { ... } while (cond);that asks for input until the user typesq. - An error-handling chain that uses a single
goto cleanup;to free two resources.
Functions and Linkage Drill
Split a single program into stats.c, stats.h, main.c:
stats.hdeclaresdouble mean(const int *a, size_t n);anddouble stdev(const int *a, size_t n);.stats.cdefines both, plus astatichelperstatic double sumsq(const int *a, size_t n, double m);.main.creads 10 integers viafgets+sscanfand prints mean and stdev.- Use
nm stats.oto confirmmeanandstdevare global (T) andsumsqis local (t).
Preprocessor Drill
Write two versions of MAX(a, b):
#define MAX(a, b) (a > b ? a : b)- show where it breaks withMAX(x & 0xF, 5).#define MAX(a, b) ((a) > (b) ? (a) : (b))- show where it still breaks withMAX(i++, j++).- Replace both with
static inline int imax(int a, int b);and explain why it is safer.
Evidence Check
This page is complete only if you can:
- build a clean multi-file program with
-Wall -Wextra -std=c11and no warnings - explain each
nmclassification for your own code - produce correct answers on the precedence and sequence-point drill without looking them up