C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - True / False Questions (Q.No. 2)
2.
In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.
Discussion:
10 comments Page 1 of 1.
Arj said:
1 decade ago
Shouldn't default args be from right to left?
Fun (int x, int y, int z =1).
Please explain.
Fun (int x, int y, int z =1).
Please explain.
Vic raghuvanshi said:
1 decade ago
Yes the default args is passed from right to left and then the cleaning of the variables are done by the called function. . .
Venkatesh said:
1 decade ago
Explain it clearly?step by step with an example program.
Manikanth said:
1 decade ago
Please anyone explain?
Ahmed said:
9 years ago
The ellipsis function has to receive at least the first argument, then the others are optional. The reason is that you access the other optional arguments as an offset of the pointer pointing to the first argument.
Pavi said:
9 years ago
Allocation is from right to left so fixed arguments should only at the end right? then how come the answer is no?
Hitesh said:
8 years ago
The variable arguments are passed like this: function(a, b, ...)
three dots are written at last after passing fixed arguments.
three dots are written at last after passing fixed arguments.
Ujjwal said:
8 years ago
If you see in command line argument, at main function two parameter is being passed one is argc i.e. int type and another is *argv [] i.e. char tye. The syntex is main (int argc, char *argv []).
My question is the reverse is possible if i interchange the position of int and char?
My question is the reverse is possible if i interchange the position of int and char?
Bavatharani said:
7 years ago
Please explain clearly.
Utkarsh said:
6 years ago
>> int something(int x, ...) { // do something here }
Calling this function like:
1. int a = something(12, 10, 11); // x = 12, and [10,11] becomes an array for you to use inside the function.
2. int b = something(12, 15); // x = 12, and [15] becomes an array for you to use inside the function.
3. int c = something(9, 5, 7, 12, 31, 2); // x = 9, and [5,7,12,31,2] becomes an array for you to use inside the function.
All above are valid. The (...) let's you use that function with as many parameters as you want (variable number of arguments).
But if you have a function like;
>> int dothis(..., int b) { // do something }
then if you call it like
>> dothis(13,14);
then the compiler cannot figure out what you want as (...) will make [13,14] into an array and b will never get a value. This results in ERROR.
Calling this function like:
1. int a = something(12, 10, 11); // x = 12, and [10,11] becomes an array for you to use inside the function.
2. int b = something(12, 15); // x = 12, and [15] becomes an array for you to use inside the function.
3. int c = something(9, 5, 7, 12, 31, 2); // x = 9, and [5,7,12,31,2] becomes an array for you to use inside the function.
All above are valid. The (...) let's you use that function with as many parameters as you want (variable number of arguments).
But if you have a function like;
>> int dothis(..., int b) { // do something }
then if you call it like
>> dothis(13,14);
then the compiler cannot figure out what you want as (...) will make [13,14] into an array and b will never get a value. This results in ERROR.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers