|
|
||||
|
||||
|
|
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|