4

I try to concatenate in the following form "string", variable, "string";

#include <stdio.h>

int main() {
        char *name = "Lannister";

        char write_letter[50] = "Here you are Mrs. ", name, " Welcome!\r\n"
                             "Getting Started\r\n"
                             "Interviews\r\n"
                             "Take-home Projects\r\n";
}

does anyone have idea how to do it?

I saw an example of sprintf(write_letter, "Here you are Mrs. %s Welcome!", name); but it is very difficult when i use large texts.

3
  • 5
    Sprintf works. Snprintf is better. Commented Sep 30, 2016 at 11:21
  • 2
    I found a "sugar" solution with macros #define NAME "Lannister" and "Here you are Mrs. " NAME " Welcome!\r\n";, but I do not know much about macros! Commented Sep 30, 2016 at 11:36
  • For a constant value, using a macro is fine. Commented Sep 30, 2016 at 13:19

4 Answers 4

4

If the name is already decided at compile time and if there is no necessity to change the name during runtime then by all means choose the simplest alternative, i.e. -

#define NAME "Lannister"

char write_letter[] = "Here you are Mrs. " NAME " Welcome!\r\n"
                      "Getting Started\r\n"
                      "Interviews\r\n"
                      "Take-home Projects\r\n";

Compile with highest warning level set. When you do that you will get a warning similar to "initializer-string for array of chars is too long" (this is the warning generated by GCC). 50 is too small for this array hence I have allowed the compiler to decide the array size (i.e. 'write_letter[]').

If you need to change the string at runtime then use either strcat() -

char write_letter[150] = "Here you are Mrs. ";
char *name = "Lannister";
char *write_letter_post = " Welcome!\r\n"
                          "Getting Started\r\n"
                          "Interviews\r\n"
                          "Take-home Projects\r\n";

strcat(write_letter, name);
strcat(write_letter, write_letter_post);
/*Use strncat() to prevent buffer overflow possibilities.*/

or, sprintf() -

char *_write_letter = "Here you are Mrs. %s Welcome!\r\n"
                      "Getting Started\r\n"
                      "Interviews\r\n"
                      "Take-home Projects\r\n";
char *name = "Lannister";
char write_letter[150];

sprintf(write_letter, _write_letter, name);
/*Use snprintf() to prevent buffer overflow possibilities.*/
Sign up to request clarification or add additional context in comments.

Comments

4

You can only use the "..." "..." notation for compile-time evaluable constant expressions. Your's is a runtime construct due to name.

You need to use strcat, snprintf etc. for that.

Comments

1

The pre-processor is able to do string concatenation for you, given that all strings involved are written directly after each other and that they are pure constant string literals. That is, the things surrounded by " ", not the variables. For example "hello" "world" will get changed to "helloworld" by the pre-processor.

This does not work with variables. If you have character arrays, you need to concatenate them with run-time functions such as strcat.

Comments

1

As an alternative to sprintf (using snprintf(NULL, 0, ...) to know how much memory to allocate for the result), you can use strcat. But again, you are responsible for allocating enough memory to contain the final string !

Comments

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.