| | |
variable arguments in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
Haha, i thought he ment passing variable arguments.. as you can pass an array as a parameter (using pointers of course), so i thought he ment how do you pass a variable to a function.
Oh well
•
•
•
•
Dear friend , I am asking about a function like printf(). as it takes the variable argument.How can we define a function like this?
printf() does not use the periods because of the format parameter which indicates the number of arguments and their data types.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>Most functions that take a variable number of arguments are declared
>with at least one real argument followed by three periods
All of them, actually.
va_start requires the last non-variadic parameter as its second argument, so at least one non-variadic parameter is required as well as the ellipsis to specify further variadic parameters.
>printf() does not use the periods because of the format parameter
>which indicates the number of arguments and their data types.
Since when? This is the prototype for printf, and I see an ellipsis:
The format string is for evaluating the variadic parameters, it's not a replacement for the required syntax.
>with at least one real argument followed by three periods
All of them, actually.
va_start requires the last non-variadic parameter as its second argument, so at least one non-variadic parameter is required as well as the ellipsis to specify further variadic parameters.>printf() does not use the periods because of the format parameter
>which indicates the number of arguments and their data types.
Since when? This is the prototype for printf, and I see an ellipsis:
C Syntax (Toggle Plain Text)
int printf ( const char *format, ... );
I'm here to prove you wrong.
It's tested now
and the output is
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdarg.h> void stringstofile (FILE *fp, ...) { char *p; va_list ap; va_start (ap, fp); while ((p = va_arg (ap, char *))) fprintf (fp, "%s\n", p); va_end (ap); } int main () { stringstofile (stdout, "one", "two", "three", NULL); return 0; }
C Syntax (Toggle Plain Text)
one two three
Last edited by TkTkorrovi; Jul 16th, 2007 at 12:43 pm. Reason: gcc
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- null pointer (VB.NET)
Other Threads in the C Forum
- Previous Thread: How to Send Data to IP address
- Next Thread: Convert MarkDown to MarkUP
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






