Re: %sequal modifier

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 03/30/04


Date: Tue, 30 Mar 2004 14:53:22 -0500 (EST)


On Tue, 30 Mar 2004, Karl Heinz Buchegger wrote:
>
> Karl Heinz Buchegger wrote:
> > Rob Somers wrote:
> > >
> > > Hi, I just came across the %sequal printf modifier,
> >
> > There is no such modifier.
> >
> > What you saw is a simple typo:
>
> Well. Actually it may not even be a typo, but be intended
>
> > >
> > > printf("d1 and d2 are %sequal!\n", d1 == d2 ? "" : "NOT ");

  I know I'm beating a dead horse, but I just wanted to point out
that the code as given is absolutely correct. Your first "correction"
was incorrect because

    printf("d1 and d2 are %s equal!\n", d1 == d2 ? "" : "NOT ");

ends up printing "d1 and d2 are equal!" or "d1 and d2 are NOT equal!",
with an extra space. Nor is

    printf("d1 and d2 are %s equal!\n", d1 == d2 ? "" : "NOT");

a valid correction, because it still leaves an extra space in the
"equal" case.

  A common variation of this technique is

    printf("You are %d year%s old.\n", age, (age==1)? "": "s");

and I recently saw Richard Heathfield post an interesting variation
on *that* technique, which I'd never use myself but which might
turn up somewhere:

    printf("You are %d year%s old.\n", age, &(age==1)["s"]);

A final variation of my own devising:

    printf("You are %d year%.*s old.\n", age, (age!=1), "s");

-Arthur