0

As described in the title, I want to concatenate the content of string var with string using Macro.

This is an example:

const char * Name = "OverFlow"
#define DEFINE_VAR(str) unsigned char u8_##str##_Var;

I want to use the macro as following:

DEFINE_VAR(Name)

The result is:

unsigned char u8_Name_Var;  

and not

unsigned char u8_OverFlow_Var;

Do you have any idea?

2
  • Please edit your question and explain what you want to achieve with this macro. Maybe there is a different solution (apart from using a macro instead of a variable as shown inmy answer). Commented Feb 26, 2021 at 9:52
  • A common programming beginner confusion is to mix up what the user sees and what the programmer sees. The user shouldn't need to know or care about the names used in the source, and those names will not be present in the final executable either. Variable names exist solely for the benefit of the programmer. That's why it doesn't make sense to name variables after user input etc. Commented Feb 26, 2021 at 10:13

3 Answers 3

3

The preprocessor cannot concatenate the value of a variable with a string it can only concatenate preprocessor tokens that may be the result of a macro expansion.

It would be possible with #define Name OverFlow or similar.

Example file macro.c:

Edit: As suggested by Lundin I added macros to get a string literal in case the variable char *Name = "OverFlow"; is needed for other purposes.

#define NAME OverFlow

#define DEFINE_VAR_2(str) unsigned char u8_##str##_Var
#define DEFINE_VAR(str) DEFINE_VAR_2(str)

/* macros to get a string literal */
#define STR_2(x) #x
#define STR(x) STR_2(x)
#define STRNAME STR(NAME)
#define STRVAR const char *Name = STR(NAME)

/* this works */
DEFINE_VAR(NAME);

/* this doesn't work */
DEFINE_VAR_2(NAME);

/* if you need a string with the variable name */
const char *Name = STRNAME;
/* or with a single macro */
STRVAR;

Result:

# 1 "macro.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "macro.c"





/* macros to get a string literal as proposed by Lundin */





/* this works */
unsigned char u8_OverFlow_Var;

/* this doesn't work */
unsigned char u8_NAME_Var;

/* if you need a string with the variable name */
const char *Name = "OverFlow";
/* or with a single macro */
const char *Name = "OverFlow";
Sign up to request clarification or add additional context in comments.

1 Comment

Also for completeness, given #define NAME OverFlow you could also add #define STR(x) #x and #define STRNAME STR(NAME) so that the source can use STRNAME when a string literal "OverFlow" is needed, like in the OP's code.
0

Ok, here's something that works but may not be what you are looking for exaclty and the gnu documentation link for you to understand C preprocessor

main.c

#include <stdio.h>
#define xstr(s) str(s)
#define str(s) "u8_"#s"_Var"

int main(int argc, char const *argv[])
{
    const char *Overflow;
    printf("name: %s\n", Overflow);
    printf("defined: %s\n", xstr(Overflow));
    return 0;
}

output

name: (null)
defined: u8_Overflow_Var

https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html & https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation

Comments

-1

The define works, what are you trying to achieve ?

const char * Name = "OverFlow";
#define DEFINE_VAR(str) unsigned char u8_##str##_Var;
DEFINE_VAR(Name)

int main(int argc, char const *argv[])
{
    printf("%s\n", Name);
    return 0;
}

output result is

OverFlow

2 Comments

Please check the result of the preprocessing, e.g. gcc -E -C input.c. As written in the question the macro expansion of DEFINE_VAR(Name) does not produce the expected result. The fact that the string pointed to by Name can be printed is not related to the problem stated in the question.
OK thanks i posted a better example and leaving this here so others can so these mistakes too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.