i want to write soemthing like this:
a function which will print various types of arguments.
I tried to work with va_list, it doesn't work.
int main(){
write("this ",1.2," is ",'a',7," test");
}
should result in:
this 1.2 is a7 test
#include <stdio.h>
int main(){ printf("%s%.1f%s%c%d%s\n", "this ", 1.2, " is ", 'a', 7, " test"); }
Name:
Anonymous2010-07-24 1:59
>>3
this isn't a multiple type function, your types are fixed.
"%s%.1f%s%c%d%s\n" specifies number&type of arguments.
Can you write it without "%s%.1f%s%c%d%s\n"?
>>6
Then you're an incompetent idiot and you should stay the fuck away from anything more complicated than a light switch.
Name:
Anonymous2010-07-24 2:18
What i want :
functionname (...){for(i=0;i<args;i++)print(eachArgument[i]);}
float a=5.4;char* b="text";int c=1;long double d=5e200;
functionname(a,b,c,d)
functionname(a,b,c)
functionname(a,b)
Is this possible in C?
>>8
yes, if print is just void print(uintmax_t n){ printf("%" PRIuMAX "\n", n); }.
if you want type-specific behavior, you have to tell the function what types you're giving it. there's already an established way to do that, which >>3 showed you.
or, you could do this: struct idiot_object{
enum type { INT, FLOAT, STR };
union value { uintmax_t i;
unsigned char c;
long double f;
char *s; }}
...
struct idiot_object a = { .type = FLOAT, .value = { .f = 5.4 }},
b = { .type = STR, .value = { .s = "text" }},
c = { .type = INT, .value = { .i = 1 }},
d = { .type = FLOAT, .value = { .f = 5e200 }};
Name:
Anonymous2010-07-24 2:50
>>10
What if the arguments are not a fixed structure? i.e. random number of argument and random type of arguments.
>>1
Learn2use the Printf method also check out what formatting signs (don't know the actual word for it in English) you need to use.
I don't remember... %d is for ints... I think.