[net.lang.c] define constants within strings

ssp@AMES-NAS.ARPA (David Coffing) (02/28/85)

Is there a way to insert a define constant into the middle of a quoted
string at compile time? I have some site-dependent menu headers and am
always annoyed when I have to "sprintf" them before using them.

#define	SITE	"Ames Research"

char	title2[] = " - Fancy Title";
char	title[80];

	sprintf(title, "%s%s", SITE, title2);
	domenu(title);

The pre-processor should have something that helps. Please repond via mail.

Chuck Collins		cpc@ames-nas     {ihnp4,hplabs}!ames!amelia!cpc

chris@umcp-cs.UUCP (Chris Torek) (03/01/85)

Since the proposed ANSI standard allows things like

	#define FOO "bar"
	#define BAR "foo"

	char baz[] = FOO " the " BAR;

I thought I'd take the opportunity to post this little program I
wrote ages ago.
-----------------------------------------------------------------------
static char sccsid[] = "@(#)string.c	U of Maryland ACT 30-Oct-1982";

/* string -- combine adjacent C strings */

#include <stdio.h>
#define	SQUOTE	'\''
#define	DQUOTE	'"'
#define	BACKSLASH '\\'

main ()
{
    register c;
    register char *p, *q;
    char buf[BUFSIZ];

    while ((c = getchar ()) != EOF) {
top:
	if (c == SQUOTE) {
	    putchar (c);
	    while ((c = getchar ()) != EOF) {
		putchar (c);
		if (c == BACKSLASH)
		    putchar (getchar ());
		else if (c == SQUOTE)
		    break;
	    }
	}
	else if (c == DQUOTE) {
	    putchar (c);
more:
	    while ((c = getchar ()) != EOF) {
		if (c == BACKSLASH) {
		    putchar (c);
		    putchar (getchar ());
		}
		else if (c == DQUOTE)
		    break;
		else
		    putchar (c);
	    }
	    p = q = buf;
	    while ((c = getchar ()) == ' ' || c == '\t' || c == '\n')
		*p++ = c;
	    if (c == DQUOTE)
		goto more;
	    putchar (DQUOTE);
	    while (q < p)
		putchar (*q++);
	    goto top;
	}
	else putchar (c);
    }
}

-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland