Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C not allow concatenating strings when using the conditional operator?

The following code compiles without problems:

int main() {     printf("Hi" "Bye"); } 

However, this does not compile:

int main() {     int test = 0;     printf("Hi" (test ? "Bye" : "Goodbye")); } 

What is the reason for that?

like image 799
José D. Avatar asked May 16 '16 17:05

José D.


People also ask

Can I concatenate strings in C?

As you know, the best way to concatenate two strings in C programming is by using the strcat() function.

Which operator is used for string concatenation in C?

The '+' operator adds the two input strings and returns a new string that contains the concatenated string.

Why should you be careful about string concatenation (+) operator in loops?

If you concatenate Stings in loops for each iteration a new intermediate object is created in the String constant pool. This is not recommended as it causes memory issues.

Is concatenation operator in C?

There is no string concatenation operator in C.

How does condition operator work in C?

If one condition is true then it will show a new and if another condition is true it will show a different value this is how a condition operator works in C. If a condition is true value will be returned it is similar to if-else loop in programming. Conditional Operator is also known as Ternary operator.

What is the use of conditional operator in JavaScript?

This conditional operator will return a true value if both the given conditions are true and Boolean. This conditional operator will return a true value if either of the given conditions is true and Boolean. This conditional operator will return a true value if both the given conditions are different.

Which operator returns a true value if the condition is false?

This conditional operator will return a true value if the given condition is false. This conditional operator will return a true value if both the given conditions are true and Boolean. This conditional operator will return a true value if either of the given conditions is true and Boolean.

Why use conditional operators instead of if-else?

Therefore, to display why conditional operators are used, you can see from the last two codes that using a conditional operator instead of using if-else can save a few lines of code.


1 Answers

As per the C11 standard, chapter §5.1.1.2, concatenation of adjacent string literals:

Adjacent string literal tokens are concatenated.

happens in translation phase. On the other hand:

printf("Hi" (test ? "Bye" : "Goodbye")); 

involves the conditional operator, which is evaluated at run-time. So, at compile time, during the translation phase, there are no adjacent string literals present, hence the concatenation is not possible. The syntax is invalid and thus reported by your compiler.


To elaborate a bit on the why part, during the preprocessing phase, the adjacent string literals are concatenated and represented as a single string literal (token). The storage is allocated accordingly and the concatenated string literal is considered as a single entity (one string literal).

On the other hand, in case of run-time concatenation, the destination should have enough memory to hold the concatenated string literal otherwise, there will be no way that the expected concatenated output can be accessed. Now, in case of string literals, they are already allocated memory at compile-time and cannot be extended to fit in any more incoming input into or appended to the original content. In other words, there will be no way that the concatenated result can be accessed (presented) as a single string literal. So, this construct in inherently incorrect.

Just FYI, for run-time string (not literals) concatenation, we have the library function strcat() which concatenates two strings. Notice, the description mentions:

char *strcat(char * restrict s1,const char * restrict s2);

The strcat() function appends a copy of the string pointed to by s2 (including the terminating null character) to the end of the string pointed to by s1. The initial character of s2 overwrites the null character at the end of s1. [...]

So, we can see, the s1 is a string, not a string literal. However, as the content of s2 is not altered in any way, it can very well be a string literal.

like image 81
Sourav Ghosh Avatar answered Oct 04 '22 04:10

Sourav Ghosh