Article View: comp.std.c
Article #2444Re: Preprocessor question
From: Rex Jaeschke
Date: Sun, 17 Feb 1991 14:04
Date: Sun, 17 Feb 1991 14:04
39 lines
1674 bytes
1674 bytes
> From: ahuttune@niksula.hut.fi (Ari Juhani Huttunen)
>
> I would like to do the following or something similar:
>
> #define BLOCK_SIZE 1024
>
> char *message = "The block size is " # BLOCK_SIZE " bytes.";
>
> I need the result: "The block size is 1024 bytes."
The following works:
---------------------------------
#define BLOCK_SIZE 1024
#define STR1(arg) #arg
#define STR2(arg) STR1(arg)
char *message = "The block size is " STR2(BLOCK_SIZE) " bytes.";
#include <stdio.h>
main()
{
printf("message = %s\n", message);
}
You must go through 2 levels of expansion. If you simply use STR1(BLOCK_SIZE)
you get `The block size is BLOCK_SIZE bytes.' instead.
Rex
----------------------------------------------------------------------------
Rex Jaeschke | Journal of C Language Translation | C Users Journal
(703) 860-0091 | 2051 Swans Neck Way | DEC PROFESSIONAL
rex@aussie.COM | Reston, Virginia 22091, USA | Programmers Journal
----------------------------------------------------------------------------
Convener of the Numerical C Extensions Group (NCEG)
----------------------------------------------------------------------------
Technical Details
Message-ID:
<55.UUL1.3#5077@aussie.COM>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!samsung!uunet!aussie!rexReferences:
<AHUTTUNE.91Feb16230548@silver-surfer.hut.fi>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!think.com!samsung!uunet!aussie!rex From: rex@aussie.COM (Rex Jaeschke) Newsgroups: comp.std.c Subject: Re: Preprocessor question Message-ID: <55.UUL1.3#5077@aussie.COM> Date: 17 Feb 91 14:04:27 GMT References: <AHUTTUNE.91Feb16230548@silver-surfer.hut.fi> Organization: Journal of C Language Translation Lines: 39