define constants within strings

Chris Torek chris at umcp-cs.UUCP
Sat Mar 2 06:08:00 AEST 1985


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 at umcp-cs		ARPA:	chris at maryland



More information about the Comp.lang.c mailing list