Can you have a function that has an argument that could be either one type or another. For example foo(x input) where x is either char or long int? should i look into enums?
Can you have a function that has an argument that could be either one type or another. For example foo(x input) where x is either char or long int? should i look into enums?
Depending on what you're doing with the input variable, you might not need to change the type. For instance, you can still pass a character to a function accepting a long int.
If you understand what you're doing, you're not learning anything.
What you're talking about modec, is called function overloading which isn't supported in C.
I'm sure there would be some way around it. (there usually is in C)
How about passing the variables in a union parameter? you would also need to also pass something that tells your function what type the argument is.
I would post some code here, but my compiler's down at the moment. (using my bro's machine) - I'm not sure if this is allowed :
Code:void foo(int type, union x{char c; long int l}) { ... }
Hmm, let me show you what I meant:
Now I'm not sure you're trying to print the character or what, but if you want to do two different things depending on the datatype of the parameter then you should really just have 2 different functions. However, if you're just going to be passing a "smaller" integer into the function then make the function parameter the "biggest" data type and go with it like I did in my code.Code:itsme@itsme:~/C$ cat fake.c #include <stdio.h> void printit(long int num) { putchar((int)num); } int main(void) { printit('A'); printit('\n'); return 0; } itsme@itsme:~/C$ gcc -Wall fake.c -o fake itsme@itsme:~/C$ ./fake A itsme@itsme:~/C$
If you understand what you're doing, you're not learning anything.
This probably won't help, but as to function overloading in C, Prelude showed us some techniques here:
http://cboard.cprogramming.com/showt...on+overloading
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
thanks everyone. itsme86 the function is exactly the same for the two types and using long for both long and char inputs works just fine.