Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two strings separated by blank being concatenated automatically

Tags:

c

I just found something very interesting which was introduced by my typo. Here's a sample of very easy code script:

printf("A" "B");

The result would be

$> AB

Can someone explain how this happens?

like image 340
Qylin Avatar asked Apr 22 '13 20:04

Qylin


People also ask

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

What happens when two strings concatenate?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Which operator when used with two strings gives a concatenated string?

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

What happens when the two strings hello and world are concatenated?

The best way to describe it is when you take two separate strings – stored by the interpreter – and merge them so that they become one. For instance, one string would be “hello” and the other would be “world.” When you use concatenation to combine them it becomes one string, or “hello world”.


1 Answers

As a part of the C standard, string literals that are next to one another are concatenated:

For C (quoting C99, but C11 has something similar in 6.4.5p5):

(C99, 6.4.5p5) "In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence."

C++ has a similar standard.

like image 88
Hunter McMillen Avatar answered Oct 13 '22 22:10

Hunter McMillen