473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generic function in C

hI,
IHave a doubt in writing a generic function in C.

Say for Example,

I need to add 2 numbers. 2 interger Numbers or 2 float Numbers using the same function add().
How do i call the add function i.e how do i send parameters.
How do i receive the parameters in add fucntion definition.

#include<stdio.h>

int main()
{
int choice;
int i1,i2;
float f1,f2;
printf("What type of number do u want to add?\n");
printf("1.Interger Nmbers\n2.Float Numbers\n Enter Choice:");
scanf("%d"&choice);

switch(choice)
{
case1:printf("Add Interger Numbers:");
scanf("%d"&i1);
scanf("%d"&i2);
/*call to a generic function add() */
/* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
add( ???);
break;

case 2: printf("Add Float Numbers:");
scanf("%d"&f1);
scanf("%d"&f2);
/*call to a generic function add() */
/* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
add( ???);
break;

}
}

/* HOW DO I RECEIVE THE PARAMETERS IN THE GENERIC FUNCTION ADD FOR BOTH FLOAT AND INT VALUES */

add(????)
{

...............
..............


}


Please Help. Its urgent.
Thanks in advance.
Mar 6 '08 #1
3 3159
gpraghuram
1,275 Expert 1GB
You can achieve the add function to add two numbers by writing a #define for the same.
Try to work on that approach and ifu need help post again
raghuram
Mar 6 '08 #2
ashitpro
542 Expert 512MB
hI,
IHave a doubt in writing a generic function in C.

Say for Example,

I need to add 2 numbers. 2 interger Numbers or 2 float Numbers using the same function add().
How do i call the add function i.e how do i send parameters.
How do i receive the parameters in add fucntion definition.

#include<stdio.h>

int main()
{
int choice;
int i1,i2;
float f1,f2;
printf("What type of number do u want to add?\n");
printf("1.Interger Nmbers\n2.Float Numbers\n Enter Choice:");
scanf("%d"&choice);

switch(choice)
{
case1:printf("Add Interger Numbers:");
scanf("%d"&i1);
scanf("%d"&i2);
/*call to a generic function add() */
/* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
add( ???);
break;

case 2: printf("Add Float Numbers:");
scanf("%d"&f1);
scanf("%d"&f2);
/*call to a generic function add() */
/* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
add( ???);
break;

}
}

/* HOW DO I RECEIVE THE PARAMETERS IN THE GENERIC FUNCTION ADD FOR BOTH FLOAT AND INT VALUES */

add(????)
{

...............
..............


}


Please Help. Its urgent.
Thanks in advance.

Use variable argument list
check the following code.
Expand|Select|Wrap|Line Numbers
  1. #include<cstdarg>
  2. #include<iostream>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. void add(char ch,...)
  8. {
  9. <spoonfed code removed-weaknessforcats>
Mar 6 '08 #3
whodgson
542 512MB
If as you indicate, you can only have one add function to service ints and floats you need to write an add() function using T (template) principles so that the compiler will write the relevant function specifics appropriate to ints or floats when they are identified for processing.
Ask yourself what does an add function do? A: it adds 2 int types or 2 float types. So this info should appear within the function parentheses. It is best to declare this function first before providing its implementation detail else where.
Mar 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
5
by: Richard Brown | last post by:
Ok, I've been looking through the .NET SDK docs and stuff. I'm wondering if you can provide a control extender that does generic validation or functionality just by dropping it on the form. For...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.