character strings, macros, ANSI c, multicharacter chars question
Karl Heuer
karl at haddock.ima.isc.com
Sun Jan 7 06:07:56 AEST 1990
In article <1990Jan4.190431.19748 at ultra.com> ted at ultra.com (Ted Schroeder) writes:
>I would like to create an integer that is full of '+' characters
>i.e. on an ASCII machine the four byte integer would be 0x2b2b2b2b.
>Unfortunately the code must run on both ASCII and EBCDIC machines.
First I'll mention the siamese character constant '++++', but only to
disrecommend it. It'll probably give you the right answer, but you can't
count on the compiler accepting it without a warning.
Barry suggested ('+'<<24 | '+'<<16 | '+'<<8 | '+'); the briefer form
(0x01010101 * '+') is equivalent. You can eliminate the byte- and word-size
assumptions with the following (note that the result is cast-free, and hence
usable at the preprocessor level, only if the compiler is ANSI):
#if __STDC__
#include <limits.h>
#else
#define UINT_MAX (~(unsigned int)0)
#define UCHAR_MAX ((unsigned char)UINT_MAX)
#endif
#define PLUS (UINT_MAX / UCHAR_MAX * '+')
Karl W. Z. Heuer (karl at haddock.isc.com or ima!haddock!karl), The Walking Lint
More information about the Comp.lang.c
mailing list