jay@argosy.UUCP (Jay O'Conor) (04/05/91)
Here's a problem that has been bugging me for a whole day now. What is wrong with this statement? fprintf( logfile, (isascii(*cp) && isprint(*cp)) ? "[%c]" : [%02x]", *cp); The problem I'm having is the result of isprint() doesn't seem to be working. *cp is defined as an unsigned char *. This is using MPW3.2b3 (from the ETO CD-ROM). If I split the isascii() and isprint() out into seperate lines, assigning their result to two temporary variables, then logical and the two results in the fprintf line, then everything is fine. Naturally, I'm not crazy about having two variables just to deal with this. The purpose of this line is to print printable characters as ascii, but print unprintable ones as hex. BTW, The statement works in Think C. Thanks Jay O'Conor MasPar Computer Corporation jay@maspar.com
steve@huxley.huxley.bitstream.com (Steve Stein) (04/05/91)
In article <1209@argosy.UUCP> jay@argosy.UUCP (Jay O'Conor) writes: > What is wrong with this statement? > > fprintf( logfile, (isascii(*cp) && isprint(*cp)) ? "[%c]" : [%02x]", *cp); > > The problem I'm having is the result of isprint() doesn't seem to be > working. *cp is defined as an unsigned char *. This is using MPW3.2b3 > (from the ETO CD-ROM). If I split the isascii() and isprint() out into > seperate lines, assigning their result to two temporary variables, then > logical and the two results in the fprintf line, then everything is > fine. Naturally, I'm not crazy about having two variables just to deal > with this. I wouldn't be surprised if this is just a bug in MPW. I recall a similar bug in THINK C (then LightspeedC) v1, which was fixed by v2 (or at least v3), having to do with register allocation in just this type of statement (a ternary expression with a compound Boolean in the first element). The complexity of the expression is compounded because isascii and isprint are macros (at least I think they are!) which themselves expand to logical expressions. One suggestion: maybe the macros are defined a bit wrong. Try changing the statement to: fprintf( logfile, ((isascii(*cp)) && (isprint(*cp))) ? "[%c]" : [%02x]", *cp); adding parens around the two macro invocations. The macros ought to have the parens in place already, but sometimes programmers get lazy. If this doesn't work, I think it's just a compiler bug. Let me know if the parens help! - Steve Stein
steve@huxley.huxley.bitstream.com (Steve Stein) (04/06/91)
In article <1209@argosy.UUCP> jay@argosy.UUCP (Jay O'Conor) writes: > What is wrong with this statement? > > fprintf( logfile, (isascii(*cp) && isprint(*cp)) ? "[%c]" : [%02x]", *cp); ^ In my previous post I didn't notice that you need a quote here^. (If your real source is indeed missing that quote, I doubt THINK C would have compiled it.) If that's your problem, ignore my previous post! Sorry. - Steve Stein