1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdarg.h>
// okay to copy/adapt

/*
 * this prints the passed numbers, up to count
 * argument type is integer
 */
void print_numbers(int count, const char* prefix, ...) {
    va_list args;
    // we are not passing the argument count, we are passing the last argument before ...
    // va_start(args, count);
    va_start(args, prefix);

    for (int i = 0; i < count; i++) {
        int argument = va_arg(args, int);
        printf("%s%d\n", prefix, argument);
    }

    va_end(args);
}

int main(int argc, char* argv[]) {
    print_numbers(4, "* ", 1, 5, 172, 8);
    printf("------\n");
    // too many arguments? ignored
    print_numbers(4, "- ",  1, 5, 172, 8, 100);
    printf("------\n");
    // too few arguments? random garbage
    print_numbers(4, "1. ",  1, 5);
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.