Bird
Raised Fist0
Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the following C code do?
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
easy
A. Prints two integers without reading input.
B. Reads one integer and prints it twice.
C. Reads two integers from input and prints them in the same order.
D. Reads two integers but prints only the first one.

Solution

  1. Step 1: Understand scanf with multiple inputs

    The scanf function reads two integers from the user input and stores them in variables a and b.
  2. Step 2: Understand printf with multiple outputs

    The printf function prints the values of a and b in the same order they were read.
  3. Final Answer:

    Reads two integers from input and prints them in the same order. -> Option C
  4. Quick Check:

    scanf and printf with two %d = same two integers [OK]
Hint: Match each %d with a variable and & in scanf [OK]
Common Mistakes:
  • Forgetting & before variables in scanf
  • Mixing order of variables in printf
  • Using wrong format specifiers
2. Which of the following is the correct syntax to read three integers a, b, c in C?
easy
A. scanf("%d %d %d", &a, &b, &c);
B. scanf("%d %d %d", a, b, c);
C. scanf("%d, %d, %d", &a, &b, &c);
D. scanf("%d %d %d", *a, *b, *c);

Solution

  1. Step 1: Check scanf format string and arguments

    The format string should have three %d separated by spaces, and variables must be passed by address using &.
  2. Step 2: Identify correct option

    scanf("%d %d %d", &a, &b, &c); uses correct format string and & before variables, so it is correct.
  3. Final Answer:

    scanf("%d %d %d", &a, &b, &c); -> Option A
  4. Quick Check:

    Use & with variables in scanf [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
  • Omitting & before variables
  • Using commas inside format string incorrectly
  • Using pointers incorrectly with *
3. What is the output of this C program?
int x, y;
scanf("%d %d", &x, &y);
printf("%d %d %d", x, y, x + y);

Input:
3 7
medium
A. 10 10 20
B. 3 7 10
C. 7 3 10
D. 3 7 37

Solution

  1. Step 1: Read input values

    scanf reads 3 into x and 7 into y.
  2. Step 2: Calculate and print output

    printf prints x (3), y (7), and x + y (3 + 7 = 10).
  3. Final Answer:

    3 7 10 -> Option B
  4. Quick Check:

    3 + 7 = 10 [OK]
Hint: Add variables inside printf for combined output [OK]
Common Mistakes:
  • Mixing order of variables in printf
  • Confusing concatenation with addition
  • Not using & in scanf
4. Find the error in this code snippet:
int a, b;
scanf("%d %d", a, b);
printf("%d %d", a, b);
medium
A. Missing semicolon after scanf.
B. Wrong format specifier in scanf.
C. printf format specifiers do not match variables.
D. Missing & before variables in scanf.

Solution

  1. Step 1: Check scanf arguments

    scanf requires addresses of variables, so &a and &b are needed.
  2. Step 2: Identify error

    Code passes variables a and b directly, causing undefined behavior or crash.
  3. Final Answer:

    Missing & before variables in scanf. -> Option D
  4. Quick Check:

    scanf needs & before variables [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
  • Forgetting & in scanf
  • Assuming variables auto-pass by address
  • Confusing scanf and printf syntax
5. Write a C program snippet that reads two integers and prints their sum and product separated by a space. Which code correctly does this?
hard
A. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a + b, a * b);
B. int a, b;
scanf("%d %d", a, b);
printf("%d %d", a + b, a * b);
C. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a - b, a / b);
D. int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a * b, a + b);

Solution

  1. Step 1: Check input reading correctness

    scanf must use &a and &b to read two integers correctly.
  2. Step 2: Check output matches sum and product

    printf should print sum (a + b) first, then product (a * b) separated by space.
  3. Final Answer:

    int a, b; scanf("%d %d", &a, &b); printf("%d %d", a + b, a * b); -> Option A
  4. Quick Check:

    scanf with & and printf sum then product [OK]
Hint: Print sum first, product second with correct scanf [OK]
Common Mistakes:
  • Forgetting & in scanf
  • Swapping sum and product order
  • Using wrong operators in printf
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard