Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

C Program Output Behavior

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is C Program Output Behavior. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

ALTEN Belgium's embedded systems interviews often include a short C program and ask the candidate to reason through it line by line, since so much embedded work comes down to correctly predicting how integer types actually behave at the byte level, not just what the code seems to say.

#include <stdio.h>

int main(void) {
    unsigned char sensor_min = 20;
    unsigned char sensor_max = 15;
    unsigned char delta = sensor_max - sensor_min;

    printf("delta: %u
", delta);

    int readings[5] = {10, 20, 30, 40, 50};
    int i = 0;
    int sum = 0;

    while (i < 5) {
        sum += readings[i];
        i++;
    }

    short average = sum / 5;
    printf("average: %d
", average);

    unsigned int a = 3;
    int b = -1;

    if (a > b) {
        printf("a is greater
");
    } else {
        printf("b is greater
");
    }

    return 0;
}

Trace through this program and state exactly what it prints to the console, in order. Explain the reasoning behind each line, including any type conversions involved.