ahuttune@niksula.hut.fi (Ari Juhani Huttunen) (02/17/91)
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." I know what I am doing wrong, but HOW should I do it? These alternatives are no good: - #define BLOCK_SIZE_STRING "1024" - char *message = "The block size is 1024 bytes."; -- ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I_ Ari Huttunen A computer is like a house of cards. Not as reliable, but it has an equal number of incompatible parts. _______________________________________________________________________________
rex@aussie.COM (Rex Jaeschke) (02/17/91)
> 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) ----------------------------------------------------------------------------
gwyn@smoke.brl.mil (Doug Gwyn) (02/18/91)
In article <AHUTTUNE.91Feb16230548@silver-surfer.hut.fi> ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: >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." >I know what I am doing wrong, but HOW should I do it? These alternatives >are no good: > - #define BLOCK_SIZE_STRING "1024" > - char *message = "The block size is 1024 bytes."; #define STRINGIZE(x) # x #define PSTRINGIZE(x) STRINGIZE(x) ... #define BLOCK_SIZE 1024 ... char message[] = "The block size is " PSTRINGIZE(BLOCK_SIZE) " bytes.";
vinoski@apollo.HP.COM (Stephen Vinoski) (04/19/91)
In article <AHUTTUNE.91Feb16230548@silver-surfer.hut.fi> ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: >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." > >I know what I am doing wrong, but HOW should I do it? These alternatives >are no good: > - #define BLOCK_SIZE_STRING "1024" > - char *message = "The block size is 1024 bytes."; How about: #define BLOCK_SIZE 1024 #define REALSTR(x) #x #define STR(x) REALSTR(x) char *message = "The block size is " STR(BLOCK_SIZE) " bytes."; -steve | Steve Vinoski (508)256-0176 x5904 | Internet: vinoski@apollo.hp.com | | HP Apollo Division, Chelmsford, MA 01824 | UUCP: ...!apollo!vinoski | | "The price of knowledge is learning how little of it you yourself harbor." | | - Tom Christiansen |