Preprocessor question

Rex Jaeschke rex at aussie.COM
Mon Feb 18 01:04:27 AEST 1991


> From: ahuttune at 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 at aussie.COM   |     Reston, Virginia 22091, USA     | Programmers Journal
----------------------------------------------------------------------------
Convener of the Numerical C Extensions Group (NCEG)
----------------------------------------------------------------------------



More information about the Comp.std.c mailing list