Clamping keeps an incoming value inside the range that later code expects.

lower bound Values below the allowed range are raised to the minimum.
upper bound Values above the allowed range are lowered to the maximum.

Range Clamp

requested
range_clamp.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

int clampScore(int requested) {
    int clamped = requested;
    if (clamped < 0) {
        clamped = 0;
    }
    if (clamped > 100) {
        clamped = 100;
    }
    return clamped;
}

int main(void) {
    int requested = 72;
    int score = clampScore(requested);
    int passing = score >= 60;

    printf("requested=%d score=%d passing=%d\n", requested, score, passing);
    return 0;
}
#include <stdio.h>

int clampScore(int requested) {
    int clamped = requested;
    if (clamped < 0) {
        clamped = 0;
    }
    if (clamped > 100) {
        clamped = 100;
    }
    return clamped;
}

int main(void) {
    int requested = -5;
    int score = clampScore(requested);
    int passing = score >= 60;

    printf("requested=%d score=%d passing=%d\n", requested, score, passing);
    return 0;
}
#include <stdio.h>

int clampScore(int requested) {
    int clamped = requested;
    if (clamped < 0) {
        clamped = 0;
    }
    if (clamped > 100) {
        clamped = 100;
    }
    return clamped;
}

int main(void) {
    int requested = 140;
    int score = clampScore(requested);
    int passing = score >= 60;

    printf("requested=%d score=%d passing=%d\n", requested, score, passing);
    return 0;
}
  1. requested ← 72

    14int main(void) {15    int requested→ 72 = 72; //@requested=-5, 14016    int score = clampScore(requested72);17    int passing = score >= 60;
  2. clamped ← 72

    3int clampScore(int requested72) {4    int clamped→ 72 = requested72;5    if (clamped < 0) {6        clamped = 0;7    }8    if (clamped > 100) {9        clamped = 100;10    }11    return clamped72;12}
  3. score ← 72, passing ← 1

    15    int requested = 72; //@requested=-5, 14016    int score→ 72 = clampScore(requested72);17    int passing→ 1 = score72 >= 60;1819    printf("requested=%d score=%d passing=%d\n", requested72, score72, passing1);20    return 0;21}
    outputrequested=72 score=72 passing=1
  1. requested ← -5

    14int main(void) {15    int requested→ -5 = -5;16    int score = clampScore(requested-5);17    int passing = score >= 60;
  2. clamped ← -5

    3int clampScore(int requested-5) {4    int clamped→ -5 = requested-5;5    if (clamped < 0) {
  3. clamped ← 0

    4int clamped = requested;5if (clamped-5 < 0) {6    clamped→ 0 = 0;7}
  4. return clamped;

    10    }11    return clamped0;12}
  5. score ← 0, passing ← 0

    15    int requested = -5;16    int score→ 0 = clampScore(requested-5);17    int passing→ 0 = score0 >= 60;1819    printf("requested=%d score=%d passing=%d\n", requested-5, score0, passing0);20    return 0;21}
    outputrequested=-5 score=0 passing=0
  1. requested ← 140

    14int main(void) {15    int requested→ 140 = 140;16    int score = clampScore(requested140);17    int passing = score >= 60;
  2. clamped ← 140

    3int clampScore(int requested140) {4    int clamped→ 140 = requested140;5    if (clamped < 0) {
  3. clamped ← 100

    7}8if (clamped140 > 100) {9    clamped→ 100 = 100;10}
  4. return clamped;

    10    }11    return clamped100;12}
  5. score ← 100, passing ← 1

    15    int requested = 140;16    int score→ 100 = clampScore(requested140);17    int passing→ 1 = score100 >= 60;1819    printf("requested=%d score=%d passing=%d\n", requested140, score100, passing1);20    return 0;21}
    outputrequested=140 score=100 passing=1