Search code examples
cc-preprocessorunsignedstringification

How to convert an unsigned define variable in an signed define variable in C


#define VERSION 1U

#define _VALUE_TO_STRING(x) #x
#define VALUE_TO_STRING(var) _VALUE_TO_STRING(var)

#define VERSION_STRING VALUE_TO_STRING(VERSION)

char readMe[] = "The current version of this document is " VERSION_STRING ".";

...

I have this part of the code where I need a global string(readMe) to be created at initialization time. The output of the above code will be -> The current version of this document is 1U. So what I want is to get rid of that "U". Is there a chance that I fix this with preprocessor functions?(like transform the unsigned defined value to an signed define value...)?


Solution

  • I don't think there is a pre-processor method to take out the U from VERSION. However, you can combine them. I would suggest:

    #define UNSIGNED_VERSION 1
    
    #define VERSION UNSIGNED_VERSION ## U
    
    #define _VALUE_TO_STRING(x) #x
    #define VALUE_TO_STRING(var) _VALUE_TO_STRING(var)
    
    #define VERSION_STRING VALUE_TO_STRING(UNSIGNED_VERSION)
    
    char readMe[] = "The current version of this document is " VERSION_STRING ".";