How to split a string sprintf into multiple lines in C?

Asked

Viewed 86 times

0

I got a string on sprintf and I would like to divide it into multiple lines, because it gets big and the code gets messy.

sprintf(srt,"GET %s HTTP/1.1\r\nAccept: text/javascript\r\nUser-Agent: Custom HTTP User Agent\r\nHost: %s\r\nConnection: Close\r\n\r\n, url, host);

How could I split this into multiple lines making the code more organized?
For example:

GET %s HTTP/1.1\r\n
Accept: text/javascript\r\n
User-Agent: Custom HTTP User Agent\r\n
Host: %s\r\n
Connection: Close\r\n
\r\n

1 answer

2


In C counting and adjacent strings are concatenated. So you can naturally split your string into several parts, each part into a row, but each part needs to have its own quotes.

Thus:

"GET %s HTTP/1.1\r\n"
"Accept: text/javascript\r\n"
"User-Agent: Custom HTTP User Agent\r\n"
"Host: %s\r\n"
"Connection: Close\r\n"
"\r\n"
  • When the constant needs to be higher, you need to know the compiler’s limits. C Standard imposes a minimum limit of 509 characters on C89. In C99 and C11 this limit was increased to 4095 characters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.