Is this valid in C?

A

Abhi

char *pc = "First" "Second";

In the compiler I checked it concates the string. I thought it would
require usage of ## preprocessor directive.

Thanks,
-Abhishikt
 
R

Richard Heathfield

Abhi said:
char *pc = "First" "Second";

In the compiler I checked it concates the string.

Correct. Adjacent string literals are catenated in TP6.
I thought it would
require usage of ## preprocessor directive.

No, that's for something else.
 
K

Keith Thompson

Abhi said:
char *pc = "First" "Second";

In the compiler I checked it concates the string. I thought it would
require usage of ## preprocessor directive.

It would be helpful if you included the question, "Is this valid in
C?", in the body of the message, not just in the subject.

Yes, this is valid. Adjacent string literals are concatentated, so
"First" "Second"
is equivalent to
"FirstSecond"

This feature is useful when you want to construct a very long literal
that won't fit on a single line. This has been valid since the 1989 C
standard.
 
R

Robert Gamble

Abhi said:
char *pc = "First" "Second";

Yes, this is valid. The preprocessor concatenates adjacent string
literals during preprocessing phase 6 (this is actually the sole
operation performed during this phase).
In the compiler I checked it concates the string. I thought it would
require usage of ## preprocessor directive.

The ## preprocessor operator is used for token concatenation.

Robert Gamble
 
P

pete

Yes, this is valid. Adjacent string literals are concatentated, so
"First" "Second"
is equivalent to
"FirstSecond"

This feature is useful when you want to construct a very long literal
that won't fit on a single line.

Also,
for if you want to use a macro expansion as part of a string.


#define LINE_LEN 63
#define str(s) # s
#define xstr(s) str(s)

rc = fscanf(fd, "%" xstr(LINE_LEN) "[^\n]%*[^\n]", line);
 
B

Brytison

Do this at compiler you'll get the result. Many sentences dependence on the
compiler.
 
K

Keith Thompson

Brytison said:
Do this at compiler you'll get the result. Many sentences dependence on the
compiler.

Please don't top-post. Your response goes below any quoted text, as
I've done here.

Concatenation of adjacent string literals is perfectly standard and
has been since the 1989 ANSI C standard. It doesn't depend on the
compiler (assuming the compiler is correct).

(Incidentally, C doesn't have "sentences"; it has statements,
declarations, expressions, and so forth.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top