Quote:
Originally Posted by NevemTeve
https://en.m.wikipedia.org/wiki/Comma_operator
Counting the commas it slow and not universal solution. How about using an array?
Code:
{
long long arr[] = { values };
size_t narr= (sizeof arr)/(sizeof arr[0]);
maxval= maxfun (narr, arr);
}
|
Yup, using an array is the way. And it lets casting the arguments!
I have this piece of code that meets the initial requirements:
- It has to work with long long int
- Don't tell the function how many arguments you are passing to it
And now I think it works fine:
Code:
#include <stdio.h>
#include <limits.h>
long long int llin_max(int num_args, long long int* args) {
int i=0;
long long int max, new_num;
max = LLONG_MIN;
for (i=0; i < num_args; i++) {
max = max > args[i]? max : args[i];
}
return max;
}
#define countparams(...) sizeof( (long long int[]) { __VA_ARGS__} ) / sizeof(long long int)
#define lli_max(...) llin_max(countparams(__VA_ARGS__), (long long int[]) {__VA_ARGS__})
int main() {
long long int num=0;
long long int a = 5000, b = 77500, c = -583;
int inta = 5000, intb = 977500, intc = -583;
num = lli_max(a, b, c);
printf("The greatest number is: %lli\n", num);
num = lli_max(inta, intb, intc);
printf("The greatest number is: %lli\n", num);
num = lli_max((5,6),(7,11),2,(50,3)); // Now this works
printf("The greatest number is: %lli\n", num);
return 0;
}