[comp.lang.c] Use of " ? : " in format param to printf

rds95@leah.Albany.Edu (Robert Seals) (01/20/89)

I have a question.

main()
{
    double sum;
    int count;

    /* ..... */

    printf(count == 0 ? " ---- " : " %4.0lf ", sum / count);
}

(If something is dopey, just ignore it because the main thing is...)

Does it matter if there are unused arguments to printf? That is, if
'count' is zero, will anything be screwed up because there is extra
crud in the code? The actual application I'm doing is much more
complicated than this (REALLY??), but the principal is the same.
Is this OK code? Lint didn't mind, nor did gcc -Wall.

rob

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/20/89)

In article <1454@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes:
>Does it matter if there are unused arguments to printf?

The proposed ANSI C Standard permits extra arguments (which are, as
always, evaluated, but otherwise unused).  It's hard to imagine an
implementation that would have problems with extra arguments to a
varargs-style function like printf().

ark@alice.UUCP (Andrew Koenig) (01/20/89)

In article <1454@leah.Albany.Edu>, rds95@leah.Albany.Edu (Robert Seals) writes:

>     printf(count == 0 ? " ---- " : " %4.0lf ", sum / count);

> Does it matter if there are unused arguments to printf?

It's OK to have unused arguments in printf, but they'll be
evaluated whether they're used or not.  Thus in this example
if count is 0, the likely result is a divide by zero error.
-- 
				--Andrew Koenig
				  ark@europa.att.com

dave@lsuc.uucp (David Sherman) (01/27/89)

In article <8801@alice.UUCP>, ark@alice.UUCP (Andrew Koenig) writes:
> >     printf(count == 0 ? " ---- " : " %4.0lf ", sum / count);
> 
> > Does it matter if there are unused arguments to printf?
> 
> It's OK to have unused arguments in printf, but they'll be
> evaluated whether they're used or not.  Thus in this example
> if count is 0, the likely result is a divide by zero error.

Well, you could always signal(SIGFPE, SIG_IGN) before this.
Kind of kludgey, but I think it would get around that problem
as long as you're running UNIX...

David Sherman
-- 
Moderator, mail.yiddish
{ uunet!attcan  att  pyramid!utai  utzoo } !lsuc!dave