0
0
Cprogramming~20 mins

Multiple input and output in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Input and Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function with multiple input parameters
What is the output of this C program when calling sum_and_product(3, 4, &sum, &product);?
C
#include <stdio.h>

void sum_and_product(int a, int b, int *sum, int *product) {
    *sum = a + b;
    *product = a * b;
}

int main() {
    int sum = 0, product = 0;
    sum_and_product(3, 4, &sum, &product);
    printf("Sum = %d, Product = %d\n", sum, product);
    return 0;
}
ASum = 7, Product = 12
BSum = 12, Product = 7
CSum = 3, Product = 4
DSum = 0, Product = 0
Attempts:
2 left
💡 Hint
Remember that pointers allow functions to modify variables outside their scope.
Predict Output
intermediate
2:00remaining
Output of function returning multiple values via struct
What will be printed by this program?
C
#include <stdio.h>

typedef struct {
    int sum;
    int diff;
} Result;

Result calculate(int x, int y) {
    Result r;
    r.sum = x + y;
    r.diff = x - y;
    return r;
}

int main() {
    Result res = calculate(10, 4);
    printf("Sum: %d, Difference: %d\n", res.sum, res.diff);
    return 0;
}
ASum: 4, Difference: 10
BSum: 6, Difference: 14
CSum: 14, Difference: 6
DSum: 10, Difference: 4
Attempts:
2 left
💡 Hint
Check how sum and difference are calculated inside the struct.
Predict Output
advanced
2:00remaining
Output of function with multiple pointer parameters and side effects
What is the output of this program?
C
#include <stdio.h>

void update_values(int *a, int *b) {
    *a = *a + *b;
    *b = *a - *b;
}

int main() {
    int x = 5, y = 3;
    update_values(&x, &y);
    printf("x = %d, y = %d\n", x, y);
    return 0;
}
Ax = 8, y = 3
Bx = 8, y = 5
Cx = 5, y = 3
Dx = 3, y = 5
Attempts:
2 left
💡 Hint
Trace the pointer updates step by step.
Predict Output
advanced
2:00remaining
Output of function returning multiple outputs via array parameters
What will this program print?
C
#include <stdio.h>

void min_max(int arr[], int size, int *min, int *max) {
    *min = arr[0];
    *max = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] < *min) *min = arr[i];
        if (arr[i] > *max) *max = arr[i];
    }
}

int main() {
    int numbers[] = {7, 2, 9, 4, 5};
    int minimum, maximum;
    min_max(numbers, 5, &minimum, &maximum);
    printf("Min: %d, Max: %d\n", minimum, maximum);
    return 0;
}
AMin: 7, Max: 5
BMin: 2, Max: 7
CMin: 4, Max: 9
DMin: 2, Max: 9
Attempts:
2 left
💡 Hint
Look carefully at how min and max are updated in the loop.
🧠 Conceptual
expert
2:00remaining
Why use pointers for multiple outputs in C functions?
Which is the main reason C functions use pointers to return multiple outputs instead of returning multiple values directly?
ABecause C functions can only return one value, pointers allow modifying variables outside the function to simulate multiple outputs.
BBecause pointers make the program run faster by avoiding copying values.
CBecause pointers automatically allocate memory for multiple return values.
DBecause pointers prevent any changes to the original variables passed to the function.
Attempts:
2 left
💡 Hint
Think about how many values a C function can return directly.