ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey can someone tell me how to put the function variable ... to use? honestly I want max function to be able to take any number of variables and tell me the maximum number. is that possible? and why do i have to put a variable before ...? it wouldn't let me just do long long int max(...). Would i have to use assembly for something like that and if so could someone show me a sample assembly program that would compile for 64 bit.
Code:
#include <stdio.h>
long long int max(int argc, ...)
{
}
int
main (int argc, char *argv[])
{
max(27, 45, 30, 98, 52);
}
like im trying to make a max function to use in c but i think i have to use assembly for it cause that gnu webpage says it cant be done with c. and I don't even want the first argument argc either. just want a function that will take any long long integers and tell me the max one.
In C, you cannot tell how many arguments were passed to the variadic function. On the other hand, in Assembly, you cannot tell how many arguments were passed to the variadic function.
https://stackoverflow.com/questions/...argc-char-argv
you can pass argc and argv directly to your max function, do not need to use va_list and friends if you don't want to.
But you need both of them (argv and argc), the pointer to the input list and the number of args. Otherwise you will not be able to handle them.
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
long long int llin_max(int num_args, ...) {
va_list valist;
int i=0;
long long int max, new_num;
va_start(valist, num_args);
max = LLONG_MIN;
for (i=0; i < num_args; i++) {
new_num = va_arg(valist, long long int);
max = max > new_num ? max : new_num;
}
va_end(valist);
return max;
}
int countparams(char *str) {
int i=0, j=0, len = strlen(str);
if (len == 0)
return 0;
for (i=0, j=1; i < len; i++)
if (str[i] == ',') j++;
return j;
}
#define lli_max(...) llin_max(countparams(#__VA_ARGS__), __VA_ARGS__);
int main() {
long long int num=0;
long long a= 5000;
long long b= 500;
num = lli_max(1,2,a, b, 300,4,5);
printf("The greatest number is: %lli\n", num);
return 0;
}
I tried passing an int argument to lli_max and it worked as intended but i think that it worked just because it's a 64 byte processor.
I think that an int as argument for lli_max it should fail on 32 byte processors.
Am I right?
In that case, will it compile without errors or warnings?
On a 64-bit platform, if you pass an int (32-bit) to a variadic function, and it fetches it as long long (64-bit), it will be a single stack entry, but the result might be different to what you expect: amd64 for example leaves the upper 32 bits uninitialized.
On a 32-bit platform (e.g. x86), va_arg(ap, long long) fetches two entries from the stack.
PS: try this: lli_max((1,2),(3,4),(5,6)) - it should be equivalent with lli_max(2,4,6)
The compiler defaults any numeric parameter to int if the function definition doesn't specify the type. So the user has to write long long int before every number, as shown in the code. (So annoying!)
Code:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
long long int llin_max(int num_args, ...) {
va_list valist;
int i=0;
long long int max, new_num;
va_start(valist, num_args);
max = LLONG_MIN;
for (i=0; i < num_args; i++) {
new_num = va_arg(valist, long long int);
max = max > new_num ? max : new_num;
}
va_end(valist);
return max;
}
int countparams(char *str) {
int i=0, j=0, parentesis=0, len = strlen(str);
if (len == 0)
return 0;
for (i=0, j=1; i < len; i++) {
if (str[i] == ',' && !parentesis) {
j++;
continue;
}
if (str[i] == '(') {
parentesis ++;
continue;
}
if (str[i] == ')')
parentesis --;
}
return j;
}
#define lli_max(...) llin_max(countparams(#__VA_ARGS__), __VA_ARGS__);
int main() {
long long int num=0;
long long a = 5000;
long long b = 77500;
long long c = -583;
/* The definition doesn't tell the compiler the type of va_arg arguments
* so it defauts any number to int */
num = lli_max(a, b, c); // works as intended, every argument is lli
printf("The greatest number is: %lli\n", num);
num = lli_max(-80,17,18, 32); // Doesn't work: arguments are int
printf("The greatest number is: %lli\n", num);
num = lli_max((long long int) -80,(long long int) 17,(long long int) 18,
(long long int) 32); // Now works
printf("The greatest number is: %lli\n", num);
return 0;
}
Is there any other way to mess the number of parameters?
By the way, I don't know the meaning of a parameter like (3,4).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.