1

Today I saw a usage of char in C as followed:

    const char temp[] = "GET / HTTP/1.0\r\n"
                        "Host:www.google.com\r\n"
                        "\r\n";

At first, I thought there would be a compile error, but actually it passed the compilation!
So can someone please tell me why it can work?
I am a fresh man learning C programming.
Thanks so much!

3
  • Adding how you compiled and with what would likely help this question, and the answer. Commented Jul 26, 2014 at 4:16
  • 1
    you try to put ";" at the end. Commented Jul 26, 2014 at 4:18
  • Sorry that I forgot to type it here...But in my code, it does exists. Commented Jul 26, 2014 at 4:19

4 Answers 4

6

If to place the missed semicolon at the end then this statement

const char temp[] = "GET / HTTP/1.0\r\n"
                    "Host:www.google.com\r\n"
                    "\r\n";

is equivalent to

const char temp[] = "GET / HTTP/1.0\r\nHost:www.google.com\r\n\r\n";

According to the C Standard in the section where translation phases are described there is written

6. Adjacent string literal tokens are concatenated

Sometines it is convinient to split a long string literal that does not fit a line into several shorter adjacent literals.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much and I did have a semicolon in my code, I forgot to type it here...Anyway, thanks a lot!
2
const char str[] = "stringstringstring";

const char str[] = "string" "string" "string";

const char str[] = "string"
              "string"
              "string";

#define NAME "string"
const char str[] = "string" NAME "string";

Will all have the same result. C concatenates adjacent strings.

Comments

1

It's a single string consisting of multiple concatenated string literals. The C language allows string literals that appear next to each other without any operator in between to be concatenated to form a single string. This is useful for string constants that span multiple lines of source, as you've seen. It's also useful when a preprocessor macro defines a string literal, you can write something like

#define BALANCE_FMT "%5.2f"
printf("Your balance is: " BALANCE_FMT "\n", balance);

Comments

1

C has string literal concatenation, meaning that adjacent string literals are concatenated at compile time; this allows long strings to be split over multiple lines, and also allows string literals resulting from C preprocessor defines and macros to be appended to strings at compile time.

For instance:

printf(__FILE__ ": %d: Hello "
       "world\n", __LINE__);

will expand to

printf("helloworld.c" ": %d: Hello "
       "world\n", 10);

which is syntactically equivalent to

printf("helloworld.c: %d: Hello world\n", 10);

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.