LearnFrenzy
Sign in Take a free test

Variable Number of Arguments · Exercise 4 of 4

Variable Number of Arguments Variable Argument Programs

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

Programs that use variadic functions and macros. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Easy

Write a variadic function that takes a count followed by that many integers and reports their sum and average.

Question page & discussion
Question 2 Easy

Write range(n, ...) that finds the smallest and largest of n double values and returns them through pointer parameters placed before the ellipsis.

Question page & discussion
Question 3 Medium

Write path_join(buf, size, part, ...) that joins parts with "/" into a buffer (ending at a NULL sentinel), without writing past the buffer.

Question page & discussion
Question 4 Medium

Write a printf-style logger with levels (DEBUG, INFO, ERROR) that drops messages below the current level and forwards the rest to vfprintf.

Question page & discussion
Question 5 Hard

Write out() that supports %d (with negative numbers), %x, %s, %c and %% using only putchar, and returns the number of characters printed.

Question page & discussion
Question 6 Hard

Write str_format() that returns a newly allocated string of exactly the right size for any printf format, and use it to build a report line.

Question page & discussion
Question 7 Medium

Write poly(x, degree, ...) that takes degree + 1 coefficients (highest power first) as doubles and evaluates the polynomial with Horner's method.

Question page & discussion
Question 8 Hard

Write show(types, ...) where each letter of the type string says what comes next: i = int, f = double, s = string, p = a struct point passed by value.

Question page & discussion
All exercises doneBack to Variable Number of ArgumentsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links