Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the C-preprocssor to convert an integer to a string? [duplicate]

Tags:

c

There's got to be a way to do this...

I have a header file, version.h with one line...

#define VERSION 9

and a few files use the defined value of VERSION as an integer. That's fine.

Without changing the way VERSION is defined, I need to build an initialized "what" string that contains that value, so I need something like this...

char *whatversion = "@(#)VERSION: " VERSION;

obviously this doesn't compile, so somehow I need to get a string of the preprocessed value of VERSION essentially giving this...

char *whatversion = "@(#)VERSION: " "9";

Any ideas? Is this possible?

like image 383
Ed. Avatar asked Mar 05 '13 22:03

Ed.


1 Answers

It is not a datatype, it is a token. A blob of text.

K & R talk about concatenating values:

 The preprocessor operator ## provides a way to concatenate actual arguments
 during macro expansion. If a parameter in the replacement text is adjacent
 to a ##, the parameter is replaced by the actual argument, the ## and
 surrounding white space are removed, and the result is re-scanned. For example,
 the macro paste concatenates its two arguments:

    #define paste(front, back) front ## back

    so paste(name, 1) creates the token name1.

-- try that. #define the string before you get to char *version=

like image 95
jim mcnamara Avatar answered Sep 30 '22 04:09

jim mcnamara