LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2004, 08:38 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
alternative printf ... multible arguements


Is there anyway to make a function prototype to take multible parameters. I'm trying to make an alternative printf, that besides formating the content, always displays the PID

e.g.

syntax
Code:
...
printj("\nHello %s. Current date is %d.", name, date);
output:
  • ...
    PID: 17966 - Hello Jay. Current date is 121304

But it must not be limited to 2 arguments. It should also take 0 or 50 arguments.
Any infomation will be greatly appriciated.

Regards jnusa

Last edited by jnusa; 12-13-2004 at 08:40 AM.
 
Old 12-13-2004, 08:43 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
man 'stdarg'


Quote:
NAME
stdarg - handle variable argument list

SYNOPSIS
#include <stdarg.h>
va_list pvar;

void va_start(va_list pvar, void parmN);

(type *) va_arg(va_list pvar, type);

void va_copy(va_list dest, va_list src);

void va_end(va_list pvar);

DESCRIPTION
This set of macros allows portable procedures that accept
variable numbers of arguments of variable types to be writ-
ten. Routines that have variable argument lists (such as
printf) but do not use stdarg are inherently non-portable,
as different machines use different argument-passing conven-
tions.

va_list is a type defined for the variable used to traverse
the list.

The va_start() macro is invoked before any access to the
unnamed arguments and initializes pvar for subsequent use by
va_arg() and va_end(). The parameter parmN is the identifier
of the rightmost parameter in the variable parameter list in
the function definition (the one just before the , ...). If
this parameter is declared with the register storage class
or with a function or array type, or with a type that is not
compatible with the type that results after application of
the default argument promotions, the behavior is undefined.

The parameter parmN is required under strict ANSI C compila-
tion. In other compilation modes, parmN need not be supplied
and the second parameter to the va_start() macro can be left
empty (for example, va_start(pvar, ). This allows for rou-
tines that contain no parameters before the ... in the vari-
able parameter list.

 
Old 12-13-2004, 08:49 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
sorry.
yes the prototype is I believe

Code:
int func ( int argc, char **argv, ... )
 
Old 12-13-2004, 08:51 AM   #4
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Ahh... very much like main's argumentlist. Hmm... I'll try to fool around with it.
Appriciate the fast reply.

 
Old 12-14-2004, 06:22 AM   #5
mayur
LQ Newbie
 
Registered: Jun 2003
Posts: 14

Rep: Reputation: 0
#define MSG(string, args...) { \
printf(string, ##args); }else

int main(void)
{
int total=0, i=0;

for (i=0;i<10;i++){
total+=i;
/* you can also use __FUNCTION__, __LINE__ */
MSG("%s : Total = %d i= %d \n",__FILE__, total, i);
}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Single IP multible domain names tangle Linux - Networking 4 11-25-2005 12:17 AM
alias command line arguements Henster Linux - Newbie 2 07-01-2005 03:40 AM
Multible read problems (Sockets) jnusa Programming 4 10-20-2004 07:28 PM
Multible newbie questions (pppd, ext2 vs ext3, and random inode read errors) (Slack10 Nicolae Slackware 8 08-28-2004 04:51 PM
installation of multible distros steelrose Linux - Newbie 4 02-21-2002 09:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration