Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2010, 01:56 PM
Pim
Guest
 
Posts: n/a
Default Macro that does printf and flushes.

Hi all.
I want to make a macro that uses printf as is
and thats flushes the output automatically.
If a macro is not possible : a subroutine.

Thank you very mutch.

Pim.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-12-2010, 02:15 PM
Nick Keighley
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

On 12 June, 14:56, Pim <m...@free.fr> wrote:

> I want to make a macro that uses printf as is
> and thats flushes the output automatically.
> If a macro is not possible : a subroutine.



that's printf() followed by fflush(stdout)? If you want to write a
printf() style function than a function would be easier than a macro.
Take a look a vprintf() that might give you ideas on how to do it.
Also look up variable arguments or the header stdarg.h in your text
book.
Reply With Quote
  #3 (permalink)  
Old 06-12-2010, 02:24 PM
Pim
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

Le Sat, 12 Jun 2010 07:15:25 -0700 (PDT),
Nick Keighley <nick_keighley_nospam@hotmail.com> disait ceci :
> On 12 June, 14:56, Pim <m...@free.fr> wrote:
>
>> I want to make a macro that uses printf as is
>> and thats flushes the output automatically.
>> If a macro is not possible : a subroutine.

>
>
> that's printf() followed by fflush(stdout)? If you want to write a
> printf() style function than a function would be easier than a macro.
> Take a look a vprintf() that might give you ideas on how to do it.
> Also look up variable arguments or the header stdarg.h in your text
> book.

YES, printf followed by fflush, sorry i forgotted to mention it.
Reply With Quote
  #4 (permalink)  
Old 06-12-2010, 02:25 PM
Pim
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

Le Sat, 12 Jun 2010 07:15:25 -0700 (PDT),
Nick Keighley <nick_keighley_nospam@hotmail.com> disait ceci :
> On 12 June, 14:56, Pim <m...@free.fr> wrote:
>
>> I want to make a macro that uses printf as is
>> and thats flushes the output automatically.
>> If a macro is not possible : a subroutine.

>
>
> that's printf() followed by fflush(stdout)? If you want to write a
> printf() style function than a function would be easier than a macro.
> Take a look a vprintf() that might give you ideas on how to do it.
> Also look up variable arguments or the header stdarg.h in your text
> book.


Right, thank you very much Nick.
I'm going to look at this.
Pim.
Reply With Quote
  #5 (permalink)  
Old 06-12-2010, 11:09 PM
Jens Thoms Toerring
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

Pim <moi@free.fr> wrote:
> Le Sat, 12 Jun 2010 07:15:25 -0700 (PDT),
> Nick Keighley <nick_keighley_nospam@hotmail.com> disait ceci :
> > On 12 June, 14:56, Pim <m...@free.fr> wrote:
> >
> >> I want to make a macro that uses printf as is
> >> and thats flushes the output automatically.
> >> If a macro is not possible : a subroutine.

> >
> > that's printf() followed by fflush(stdout)? If you want to write a
> > printf() style function than a function would be easier than a macro.
> > Take a look a vprintf() that might give you ideas on how to do it.
> > Also look up variable arguments or the header stdarg.h in your text
> > book.

> YES, printf followed by fflush, sorry i forgotted to mention it.


Mmm, if you want to have the equivalent of fflush() after each
and every printtf() then you might consider using the setbuf()
or setvbuf() function to simply switch buffering off (and on
again) for the standard output - that avoids having to call
two functions every time.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Reply With Quote
  #6 (permalink)  
Old 06-13-2010, 02:13 AM
Ben Bacarisse
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

jt@toerring.de (Jens Thoms Toerring) writes:

> Pim <moi@free.fr> wrote:

<snip>
>> YES, printf followed by fflush, sorry i forgotted to mention it.

>
> Mmm, if you want to have the equivalent of fflush() after each
> and every printtf() then you might consider using the setbuf()
> or setvbuf() function to simply switch buffering off (and on
> again) for the standard output - that avoids having to call
> two functions every time.


The OP should note the down side as well: *all* output in stdout will
then be un-buffered which might be too large a cost to bear if a lot of
calls to character output functions are made.

--
Ben.
Reply With Quote
  #7 (permalink)  
Old 06-13-2010, 03:29 PM
Charles Keepax
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

A macro should be possible assuming your compiler supports variadic
macros, no promises on that but I think most do these days. I believe
something like this should work:

#define flushyprint(...) {printf(__VA_ARGS__); fflush(stdout);}

Obviously you will need stdarg.h and stdio.h included anywhere you are
going to use it.

Charles Keepax
Reply With Quote
  #8 (permalink)  
Old 06-13-2010, 03:58 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

On 6/13/2010 11:29 AM, Charles Keepax wrote:
> A macro should be possible assuming your compiler supports variadic
> macros, no promises on that but I think most do these days. I believe
> something like this should work:
>
> #define flushyprint(...) {printf(__VA_ARGS__); fflush(stdout);}
>
> Obviously you will need stdarg.h and stdio.h included anywhere you are
> going to use it.


No need for <stdarg.h>. Also, if doing this with a macro it
would be a good idea to use the "do {...} while(0)" trick so

if (whatnot)
flushyprint(whatever);
else
whatyouwill();

will compile. (As shown, the stray ";" would mess things up.)

Personally, my first choice would be to use setvbuf() and make
stdout unbuffered. If that's impossible or impractical, my second
choice would be to use a function:

int flushyprint(const char *format, ...) {
va_list ap;
int result;
va_start(ap, format);
result = vprintf(format, ap);
va_end(ap);
fflush(stdout);
return result;
}

For this version you *do* need <stdarg.h> -- but only where the
function is defined, not every place it's called.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Reply With Quote
  #9 (permalink)  
Old 06-13-2010, 04:10 PM
Ben Bacarisse
Guest
 
Posts: n/a
Default Re: Macro that does printf and flushes.

Charles Keepax <ckeepax@gmail.com> writes:

> A macro should be possible assuming your compiler supports variadic
> macros, no promises on that but I think most do these days. I believe
> something like this should work:
>
> #define flushyprint(...) {printf(__VA_ARGS__); fflush(stdout);}


The trouble with that is that flushyprint("x") and flushyprint("x"); are
syntactically distinct. For example:

if (C)
flushyprint("s1");
else
flushyprint("s2");

is a syntax error. It's a shame to have to remember when a ; is
permitted.

In this case, where the two parts are both expressions, you can use:

#define flushyprint(...) (printf(__VA_ARGS__), fflush(stdout))

Another solution is to write

#define flushyprint(...) do {printf(__VA_ARGS__), fflush(stdout);} while(0)

Though the result is now not an expression.

In C89, the usual plan (if it must be a macro) is to write:

#define flushyprint(args) (printf args, fflush(stdout))

and to insist that callers write:

flushyprint(("X=%d\n", x));

Not ideal, but it works.

Since the cost of a function call will usually be lost in doing IO, a
variadic function is probably the most portable solution.

--
Ben.
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 09:12 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.