Variable Number of Arguments for C Programming MCQ Practice

Variable Number of Arguments for C Programming MCQ Practice

Examination Preparation and Interview questions on Variable Number of Arguments for C Programming

What you’ll learn

  • Variable Number of Arguments for C Programming
Requirements
  • Anyone who wants to learn C Programming
Description

Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.

1) Sum of given numbers

2) Minimum of given numbers

and many more. Variable number of arguments are represented by three dotes (…)

To portably implement variadic functions in the C programming language, the standard stdarg.h header file is used. The older varargs.h header has been deprecated in favor of stdarg.h

Note that the function does not know the number of arguments or their types. The function expects that the types will be int, and that the number of arguments is passed in the first argument (this is a frequent usage but by no means enforced by the language or compiler). In some other cases, for example printf, the number and types of arguments are figured out from a format string. In both cases, this depends on the programmer to supply the correct information. If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the format string attack

stdarg.h declares a type, va_list, and defines four macros: va_start, va_arg, va_copy, and va_end. Each invocation of va_start and va_copy must be matched by a corresponding invocation of va_end. When working with variable arguments, a function normally declares a variable of type va_list (ap in the example) that will be manipulated by the macros

va_start takes two arguments, a va_list object and a reference to the function’s last parameter (the one before the ellipsis; the macro uses this to get its bearings). It initialises the va_list object for use by va_arg or va_copy. The compiler will normally issue a warning if the reference is incorrect (e.g. a reference to a different parameter than the last one, or a reference to a wholly different object), but will not prevent compilation from completing normally

va_arg takes two arguments, a va_list object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of va_arg allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument

va_end takes one argument, a va_list object. It serves to clean up. If you wanted to, for instance, scan the variable arguments more than once, you would re-initialise your va_list object by invoking va_end and then va_start again on it

va_copy takes two arguments, both of them va_list objects. It clones the second (which must have been initialised) into the first. Going back to the “scan the variable arguments more than once” example, this could be achieved by invoking va_start on a first va_list, then using va_copy to clone it into a second va_list. After scanning the variable arguments a first time with va_arg and the first va_list (disposing of it with va_end), you could scan the variable arguments a second time with va_arg and the second va_list. Don’t forget to va_end the clone va_list

These questions will give you basic idea for Examination Preparation and/or interview on Variable Number of Arguments for C Programming.

Please Note:

  1. These questions are only for practice and understanding level of knowledge only. It is not necessary that these questions may or may not appear for examinations and/or interview questions

  2. In this practice test, because of large amount of questions (around 26 questions) some of questions may have repeated

  3. I had to put as 70% pass rate because there may also be wrong answers from my side.

Who this course is for:
  • C Programming
  • Variable Number of Arguments for C Programming

Tags:

Tutorial Bar
Logo