C Macros to create strings

I would like to use C #define to build literal strings at compile time.

The string are domains that change for debug, release etc.

I would like to some some thing like this:

#ifdef __TESTING
    #define IV_DOMAIN domain.org			//in house testing
    #define IV_DOMAIN test.domain.com		//live testing servers
    #define IV_DOMAIN domain.com			//production

// Sub-Domain
#define IV_SECURE "secure.IV_DOMAIN"             //secure.domain.org etc

But the preprocessor doesn’t evaluate anything within “”

  1. Is there a way around this?
  2. Is this even a good idea?

Concatenating strings in macros – C++

What’s the easiest way to concatenate strings defined in macros. i.e. The pseudo code I’m looking for would be like: #define ROOT_PATH /home/david/ #define INPUT_FILE_A ROOT_PATH+data/inputA.bin #

Mixing C/C++ using macros and C style strings

gcc/g++ 4.7.2 CXXFLAGS -Wall -Wextra -g -O2 Hello, I have this header file (mu_test.h) that is written using C style. It contain the following marcos #define GET_ERROR() ((errno == 0) ? None : stre

How to create more Macros in C# Post build step

I have an Visual Studio 2012 Solution that includes both C# and C++ projects. I want to create a postbuild step and I notice that the C++ projects have much more macros than the C# projects. I need to

Generating function names with strings using macros in C

I’ve two functions: void foo0(int bar); void foo1(int bar); I wan’t to be able create a macro QUX that will expand to these function names based on another macro BAZ. I tried the following: #define B

Structure packing with C macros

I’m trying to implement #pragma pack directive through _Pragma Š¾perator, to create macros for data structure alignment. All this is described in the book in the book C: In a Nutshell #define STR(s)

C++ create a counter macros increasing along all files

I am trying to define a macros that increases along all source files of my project. Unlike the __COUNTER__ macros which is separate for all files. I tried using a static member variable in a class som

C Switch/case macros, multiple cases

(I know most people are going to say it’s horrible). I have written the following macros to easily write switchs using strings instead of if/else if/else : #define str_switch( value ) / do { / const c

Listing C Constants/Macros

Is there a way to make the GNU C Preprocessor, cpp (or some other tool) list all available macros and their values at a given point in a C file? I’m looking for system-specific macros while porting a

C++ nested macros?

Is there any way, in C++, to define nested macros/constants within a class, in a similiar fashion to nested typedefs, or a method to acheive similiar functionality? The motive is generally for the mac

Macros and Visual C++

I’m trying to get a better understanding of what place (if any) Macros have in modern C++ and Visual C++, also with reference to Windows programming libraries: What problem (if any) do Macros solve in


Try using the ## operator

#define IV_SECURE secure.##IV_DOMAIN

Strings that are next together are combined by the C compiler.

#define DOMAIN "example.com"
#define SUBDOMAIN "test." DOMAIN
const char *asCString = SUBDOMAIN;
NSString *asNSString = @SUBDOMAIN;

What you need are the # and ## operators, and automatic string concatenation.

The # preprocessing operator turns the macro parameter into a string. The ## operator pastes two tokens (such as macro parameters) together.

The possibility that comes to mind to me is

#define IV_DOMAIN domain.org
#define IV_SECURE(DOMAIN) "secure." #DOMAIN

which should change IV_SECURE to

#define IV_SECURE "secure." "domain.org"

which will automatically concatenate to “secure.domain.org” (assuming the phases of translation are the same in C as C++).

ANOTHER EDIT: Please, please read the comments, which show how I’ve managed to get confused. Bear in mind that I am thoroughly experienced in C, although perhaps a touch rusty. I would delete this answer, but I thought I’d leave it as an example of how easy it is to get confused by the C preprocessor.

In C, string literals are concatenated automatically. For example,

const char * s1 = "foo" "bar";
const char * s2 = "foobar";

s1 and s2 are the same string.

So, for your problem, the answer (without token pasting) is

#ifdef __TESTING
    #define IV_DOMAIN "domain.org"
    #define IV_DOMAIN "test.domain.com"
    #define IV_DOMAIN "domain.com"

#define IV_SECURE "secure." IV_DOMAIN
#define IV_MOBILE "m." IV_DOMAIN

As others have noted, use token pasting. You should also be aware that macro names like


are reserved in C (don’t know about Objective C) for the implementation – you are not allowed to use them in your own code. The reserved names are anything containing double underscores and anything begining with an underscore and an uppercase letter.

There are a couple ways to do this:

  1. if you’re dealing with only string literals, you can simply use simply use strings – placing one string literal after another causes the compiler to concatenate them.

  2. if there may be other things than string literals involved (ie., you are creating new identifiers from the macros) use the ‘##” preprocessor token pasting operator. You’d probably also need to use the ‘#’ ‘stringizing operator to make your macros into literal strings.

An example of #1:

#ifdef __TESTING
    #define IV_DOMAIN "domain.org"                        //in house testing
    #define IV_DOMAIN "test.domain.com"           //live testing servers
    #define IV_DOMAIN "domain.com"                        //production

// Sub-Domain
#define IV_SECURE "secure." IV_DOMAIN          //secure.domain.org etc
#define IV_MOBILE "m." IV_DOMAIN

And as far as the token pasting operator goes, I don’t think that most of the answers that suggested using the token pasting preprocessor operator have actually tried it – it can be tricky to use.

Using the answer that is often suggested will result in a compiler error when you try to use the IV_SECURE macro, because:

#define IV_SECURE "secure."##IV_DOMAIN

expands to:


You might want to try to use the ‘#`’ ‘stringizing’ operator:

#define IV_SECURE "secure." #IV_DOMAIN

But that won’t work because it only works on macro arguments – not just any old macro.

one thing to be aware of when you’re using the token-paste (‘##’) or stringizing (‘#’) preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases.

If you don’t do this and the items passed to the token-pasting operator are macros themselves, you’ll get results that are probably not what you want:

#include <stdio.h>

#define STRINGIFY2( x) #x
#define PASTE2( a, b) a##b
#define PASTE( a, b) PASTE2( a, b)

#define BAD_PASTE(x,y) x##y
#define BAD_STRINGIFY(x) #x

#define SOME_MACRO function_name

int main() 
    printf( "buggy results:/n");
    printf( "%s/n", STRINGIFY( BAD_PASTE( SOME_MACRO, __LINE__)));
    printf( "%s/n", BAD_STRINGIFY( BAD_PASTE( SOME_MACRO, __LINE__)));
    printf( "%s/n", BAD_STRINGIFY( PASTE( SOME_MACRO, __LINE__)));

    printf( "/n" "desired result:/n");
    printf( "%s/n", STRINGIFY( PASTE( SOME_MACRO, __LINE__)));

The output:

buggy results:

desired result:

So using your original IV_DOMAIN defines and the utilty macros from above, you could do this to get what you want:

// Sub-Domain
#define IV_SECURE "secure." STRINGIFY( IV_DOMAIN)   //secure.domain.org etc

I see lots of good and correct answers to your first question, but none to your second, so here’s this: I think this is a terrible idea. Why should you have to rebuild your software (particularly the release version) just to change the server name? Also, how will you know which version of your software points at which server? You’ll have to build in a mechanism to check at runtime. If it’s at all practical on your platform, I recommend you load the domains/URLs from a config file. Only the smallest of embedded platforms may not be “practical” for that purpose šŸ™‚