Quantcast
Channel: Complete, Concrete, Concise » C
Viewing all articles
Browse latest Browse all 11

Programming Error – Unintended String Concatenation

$
0
0

In an effort to be helpful, C and C++ compilers concatenate adjacent strings together.

In the following example, the compiler will concatenate the two strings into a single string:

printf("This is a string " "and this is another string.");

Which is probably what you intended anyway.

Because C and C++ are free form languages, adjacent strings can appear in many different formats – as this string table shows:

char *array[] = {
    "str_1",
    "str_2",
    "str_3"
    "str_4",
    "str_5",
    NULL};

Did you notice the missing comma (,) after "str_3"? It can be a hard one to spot.

This is the type of bug that can result in something like a simple parser failing:

if (strcmp(array[i], test_string) == 0)

because str_3 and str_4 will never be matched, but the others will.

Please consider sharing this article if you liked it or found it useful.

You may use one of the buttons below or share using your own favourite media.

Please do not republish this article on your website. You may publish a short excerpt and provide a link back to the original article.


Viewing all articles
Browse latest Browse all 11

Trending Articles