473,385 Members | 1,869 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,385 software developers and data experts.

How to declare user defined function with variant no of arguments in c? like printf.

for example the printf function can take different no of arguments:
printf("%d%d",x,y); //here it takes only 3 arguments
printf("%d%d%d",a,b,c); // n here it takes 4 argumets

how can we declare user defined function like that, that can take many arguments,

i know when we use argv, argc as arguments in the main function. but i want a non main function behave like that.
Dec 9 '10 #1
2 2825
Banfa
9,065 Expert Mod 8TB
Yep you can declare a varidac function just like printf and scanf.

You use the ellipsis to the the compiler that a variable number of arguments with variable type follows, this is how printf is declared with an ellipsis int printf ( const char * format, ... );.

In the code of the function you use the macros in stdarg.h (or cstdarg for C++) to access the variables passed.
Dec 9 '10 #2
johny10151981
1,059 1GB
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. void sum(char *msg, ...)
  4. {
  5. int total = 0;
  6. va_list p;
  7. int arg;
  8. va_start(p, msg);
  9. while ((arg = va_arg(p,int)) != 0) {
  10. total += arg;
  11. }
  12. printf(msg, total);
  13. va_end(p);
  14. }
  15.  
follow the above simple example

It will add up all integer numbers.

But if you send String instead of integer you wont get any error/warning message.

If you want to manage various type variable you will have to think something like printf function
Dec 10 '10 #3

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

Similar topics

2
by: Erik Grob \(MCP\) | last post by:
Does anyone know where to find or how to write a quick user defined fucntion that will return a table object when passed the string name of the table object. The reason why I want dynamicallly set...
3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
2
by: Highlander416 | last post by:
This is driving me crazy. I need to create a UDF that would return a TRUE/FALSE (bit) value based on a comparison it does. CREATE FUNCTION dbo.SelectedByApplication ( @ApplicationID int,...
3
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
0
by: bprasanth_20 | last post by:
Hi, I need an urgent help. I need to create a UDF (User Defined Function) in DB2 SQL which can accept any number of arguments (from 2 to 5 arguments). I do not how to achieve this. When I pass 2...
0
by: cuddles | last post by:
hi all i need to declare a temporary table within a user-defined function in IBM DB2 ver 8. can someone give me the syntax for the temp table declaration. CREATE FUNCTION PEGASUS.readOrder(...
0
by: cognoid | last post by:
HI, I am completely new to DB2 and I am trying to get this user defined function to work. It doesn't seem to like the way I am using my input variables in the body of the function. I get the...
1
by: qwedster | last post by:
Hola! In the following code snippet, I am creating User Defined Function (T-SQL) programmatically into database from C# Code: BTW, this code originally I wrote for creating StoredProcedure...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.