[comp.lang.c] character strings, macros, ANSI c, multicharacter chars question

ted@ultra.com (Ted Schroeder) (01/05/90)

I have a macro that needs to turn an argument into a character string.
I notice in IBM/370 C there is a # operator that will do just this.
Is this proper ANSI C?
Here's an example:

    #define PRINTS(a) printf("%s\n", #a);


Also, 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.  
Right now I do the following

    #ifdef ASCII
    #define PLUS 0x2b2b2b2b
    #else /* EBCDIC */
    #define PLUS 0x4e4e4e4e
    #endif /* ASCII */

Is there any portable way to do this?

Thanks in advance for your help.

      Ted Schroeder                   ted@Ultra.com
      Ultra Network Technologies      ...!ames!ultra!ted
      101 Daggett Drive           
      San Jose, CA 95134          
      408-922-0100

Disclaimer:  I don't even believe what I say, why should my company?

barmar@think.com (Barry Margolin) (01/05/90)

In article <1990Jan4.190431.19748@ultra.com> ted@ultra.com (Ted Schroeder) writes:
>Also, 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.  
>Is there any portable way to do this?

I don't think this can be done fully portably, because you can't assume
that integers and chars are 32 and 8 bits wise, respectively.  If you're
will to be portably only to such systems (which is better than your
previous solution), I think the following will do it:

	'+'<<24 | '+'<<16 | '+'<<8 | '+'

You could also create a union of an integer and a four-character array full
of '+' characters.  This also isn't truly portable, but it will also work
on many systems.
--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

karl@haddock.ima.isc.com (Karl Heuer) (01/07/90)

In article <1990Jan4.190431.19748@ultra.com> ted@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@haddock.isc.com or ima!haddock!karl), The Walking Lint