Adam
Adam

Reputation: 21

Is there an easy way to concatenate string non-literals with literals?

I would like to be able to do this:

lcd_putc("\fDeposited $" & disp_money & "\nAdd $" & temp & " more");

Unfortunately, the string literals and non-literals don't concatenate that easily. I know how to concatenate two literals, and how to concatenate two non-literals (with strcat) but that's not really what I'm looking for. Can anyone help?

Upvotes: 1

Views: 337

Answers (6)

John Knoeller
John Knoeller

Reputation: 34148

Strings are not first class objects in C, so there is no language support for concatenation in the way you describe. There are, however, a lot of different functions that you can call that give you the output you need.

Probably the easiest to work with is sprintf. Be aware that you must supply an output buffer large enough to hold the resultant string, and sprintf doesn't do any bounds checking, if you have access to snprintf or sprintf_s then use that instead.

char output[SUFFICIENTLY_LARGE_VALUE];
sprintf (output, "\fDeposited $%s\nAdd $%s more", disp_money, temp);

%s in the sprintf string represents a place where a string parameter will be inserted, the first % is the first argument (disp_money) and the second % is the second argument (temp).

Upvotes: 0

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19467

Memory management in C is manual. To concatenate the strings, you will need to provide an array variable with enough characters to hold the final result. You can allocate this buffer variable on the stack, as a local variable to your function, or on the heap, using malloc().

Or you can avoid allocating the buffer by avoiding performing concatenation, if what you intend to do is display the strings. In that case, the following:

lcd_putc("\fDeposited $");
lcd_putc(disp_money);
lcd_putc("\nAdd $");
lcd_putc(temp);
lcd_putc(" more");

is a simple way to write what you could use. This method has the disadvantage of incurring the overhead (if any) due to lcd_putc(). This method has the advantage of not require concatenation of strings.

If and when you do need to concatenate strings, you will want to use snprintf() to ensure that you do not overflow your buffer (see the name of this web site), so do not use sprintf().

Just to show you the way for that future day when you need to concatenate strings:
#define STR_DEPOSITED "\fDeposited $"
#define STR_ADD "\nAdd $"
#define STR_MORE " more"
int total_length = strlen(STR_DEPOSITED) + strlen(STR_ADD) + strlen(STR_MORE) + strlen(disp_money) + strlen(temp) + 1;
char * buffer = malloc(total_length + sizeof(char));
snprintf(buffer, "%s%s%s%s%s", STR_DEPOSITED, disp_money, STR_ADD, temp, STR_MORE);

You can also accomplish the same thing using strncpy(), strncat(). As a side note, also consider using strnlen() on the variables, in order not to read beyond the end of non-terminated buffer.

Upvotes: 0

Eric
Eric

Reputation: 19863

I think you might be searching for sprintf

Example:

char string[50];
int file_number = 0;

sprintf( string, "file.%d", file_number );
file_number++;
output_file = fopen( string, "w" );

Upvotes: 0

wallyk
wallyk

Reputation: 57774

strcat is the way to do it. For more advanced concatenation, consider sprintf:

sprintf (buf, "\fDeposited $%s\nAdd $%s more", disp_money, temp);

Upvotes: 1

Ben Collins
Ben Collins

Reputation: 20686

char *buf = (char*)calloc(1,512);

sprintf(buf, "\fDeposited $%0.2d\nAdd $%0.2d more", disp_money, temp);

?

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

sprintf() and snprintf() are good for this.

Upvotes: 2

Related Questions