Functions
- What will be the output of the following C code?
#include <stdio.h>
#include <stdarg.h>
void function(int, ...);
int main()
{
function(21, 13, 15, 17, 101, 113);
return 0;
}
void function(int n, ...)
{
int num, k = 0;
va_list start;
va_start(start, n);
while (k != 5)
{
num = va_arg(start, int);
k++;
}
printf("%d", num);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
113
- The standard header _______ is used for variable list arguments (…) in C.
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
<stdarg.h>
- What is the purpose of va_end?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Cleanup is necessary & Must be called before the program returns
- Which of the following data-types are promoted when used as a parameter for an ellipsis?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
char
- What will be the output of the following C code?
#include <stdio.h>
#include <stdarg.h>
int fun(...);
int main()
{
char ch = 99;
fun(ch);
return 0;
}
int fun(...)
{
va_list list;
char ch = va_arg(list, char);
printf("%c\n", ch);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Compilation Error
main.c:3:13: error: ISO C requires a named argument before ‘...’
int fun(...);
^~~
main.c:10:13: error: ISO C requires a named argument before ‘...’
int fun(...)
^~~
In file included from main.c:2:0:
main.c: In function ‘fun’:
main.c:13:32: warning: ‘char’ is promoted to ‘int’ when passed through ‘...’
char ch = va_arg(list, char);
^
main.c:13:32: note: (so you should pass ‘int’ not ‘char’ to ‘va_arg’)
main.c:13:32: note: if this code is reached, the program will abort