LearnFrenzy
Sign in Take a free test

Variable Number of Arguments · Exercise 2 of 4

Variable Number of Arguments Predict the Output

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each program and choose what it prints. Every program was compiled and run, so the answers are the real output.

Question 1 Easy

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

int largest(int n, ...) {
    va_list ap;
    va_start(ap, n);
    int best = va_arg(ap, int);
    for (int i = 1; i < n; i++) {
        int v = va_arg(ap, int);
        if (v > best)
            best = v;
    }
    va_end(ap);
    return best;
}

int main(void) {
    printf("%d %d\n", largest(4, 12, 99, -5, 40), largest(2, -8, -3));
    return 0;
}
A 40 -8
B 99 -3
C 99 -8
D 12 -3
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

int total(int n, ...) {
    va_list ap;
    va_start(ap, n);
    int t = 0;
    while (n-- > 0)
        t += va_arg(ap, int);
    va_end(ap);
    return t;
}

int main(void) {
    printf("%d\n", total(2, 5, 10, 100, 1000));
    return 0;
}
A 1115
B 0
C 15
D Compilation error
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

void join(const char *sep, ...) {
    va_list ap;
    va_start(ap, sep);
    const char *s = va_arg(ap, const char *);
    int first = 1;
    while (s != NULL) {
        printf("%s%s", first ? "" : sep, s);
        first = 0;
        s = va_arg(ap, const char *);
    }
    va_end(ap);
    printf("\n");
}

int main(void) {
    join(" > ", "Home", "Programming", "C", (char *) NULL);
    return 0;
}
A > Home > Programming > C
B Home > Programming > C >
C Home > Programming > C
D Home
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

void dump(int n, ...) {
    va_list ap;
    va_start(ap, n);
    for (int i = 0; i < n; i++)
        printf("%d ", va_arg(ap, int));
    va_end(ap);
    printf("\n");
}

int main(void) {
    char c = 'a';
    short s = -2;
    _Bool b = 1;
    dump(3, c, s, b);
    return 0;
}
A a -2 1
B 97 65534 1
C 97 -2 1
D 97 -2 true
Question page & discussion
Question 5 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

double average(int n, ...) {
    va_list ap;
    va_start(ap, n);
    double sum = 0;
    for (int i = 0; i < n; i++)
        sum += va_arg(ap, double);
    va_end(ap);
    return sum / n;
}

int main(void) {
    float a = 1.5f, b = 2.5f;
    printf("%.2f %.2f\n", average(2, a, b), average(3, 1.0, 2.0, 6.0));
    return 0;
}
A 2.00 2.00
B 2.00 3.00
C 0.00 3.00
D 2.50 3.00
Question page & discussion
Question 6 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

int fit(char *buf, size_t size, const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int n = vsnprintf(buf, size, fmt, ap);
    va_end(ap);
    return n;
}

int main(void) {
    char b[8];
    int n = fit(b, sizeof b, "%s-%04d", "INV", 42);
    printf("%d [%s]\n", n, b);
    return 0;
}
A 8 [INV-004]
B 7 [INV-004]
C 8 [INV-0042]
D 3 [INV]
Question page & discussion
Question 7 Hard

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

void percent(int n, ...) {
    va_list ap, ap2;
    va_start(ap, n);
    va_copy(ap2, ap);
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += va_arg(ap, int);
    for (int i = 0; i < n; i++)
        printf("%d%% ", va_arg(ap2, int) * 100 / sum);
    va_end(ap2);
    va_end(ap);
    printf("\n");
}

int main(void) {
    percent(3, 1, 1, 2);
    return 0;
}
A 1% 1% 2%
B 33% 33% 66%
C 25% 25% 50%
D 0% 0% 0%
Question page & discussion
Question 8 Medium

What is the output of the following C program?

main.c
#include <stdarg.h>
#include <stdio.h>

void print_all(const char *types, ...) {
    va_list ap;
    va_start(ap, types);
    for (const char *t = types; *t; t++) {
        if (*t == 'i') printf("%d", va_arg(ap, int));
        else if (*t == 'd') printf("%.1f", va_arg(ap, double));
        else if (*t == 's') printf("%s", va_arg(ap, char *));
        printf(t[1] ? ", " : "\n");
    }
    va_end(ap);
}

int main(void) {
    print_all("sid", "C", 23, 9.75);
    return 0;
}
A C, 23, 9.7
B C 23 9.75
C C, 23, 9.8
D C, 23, 9.75
Question page & discussion
Question 9 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

#define NARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
#define SUM3(a, ...) ((a) + NARGS(__VA_ARGS__))

int main(void) {
    printf("%zu %zu %zu\n", NARGS(7), NARGS(1, 2, 3, 4), SUM3(10, 0, 0));
    return 0;
}
A 1 4 3
B 7 10 12
C 1 4 10
D 1 4 12
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

#define CHECK(...) printf("%s -> %d\n", #__VA_ARGS__, (__VA_ARGS__))

int main(void) {
    int x = 5;
    CHECK(x > 3 && x < 10);
    CHECK(x % 2, x * 2);
    return 0;
}
A x > 3 && x < 10 -> 1 x % 2, x * 2 -> 1
B 1 -> 1 10 -> 10
C x > 3 && x < 10 -> 1 x % 2, x * 2 -> 10
D x > 3 && x < 10 -> 1 x % 2 -> 1
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links