string literal declaration

T

Tagore

Hi,

I was trying to understand some code and came across a string
literal definition as:
char a[]="abc""def"; // string-1

I had seen eariler only string literal with characters in a single
pair of quotes(") e.g.
char a[]="abcdef"; // string-2

Please clear why string-1 is not showing syntax error and how it is
same as string-2.

Thanks,
 
I

Ian Collins

Hi,

I was trying to understand some code and came across a string
literal definition as:
char a[]="abc""def"; // string-1

I had seen eariler only string literal with characters in a single
pair of quotes(") e.g.
char a[]="abcdef"; // string-2

Please clear why string-1 is not showing syntax error and how it is
same as string-2.

The preprocessor will concatenate them.
 
P

Peter Nilsson

Ian Collins said:
    I was trying to understand some code and came across a
string literal definition as:
char a[]="abc""def";    // string-1

   I had seen eariler only string literal with characters
in a single pair of quotes(") e.g.
  char a[]="abcdef";     // string-2

Please clear why string-1 is not showing syntax error
and how it is same as string-2.

Because string literals are concatenated before syntactic and
semantic analysis.
The preprocessor will concatenate them.

Nit: The compiler will concatenate them. Preprocessors are
usually limited to translation phases 1 through 4. String
concatenation is translation phase 6 [cf 5.1.1.2.]
 
N

Nick

Peter Nilsson said:
Ian Collins said:
    I was trying to understand some code and came across a
string literal definition as:
char a[]="abc""def";    // string-1

   I had seen eariler only string literal with characters
in a single pair of quotes(") e.g.
  char a[]="abcdef";     // string-2

Please clear why string-1 is not showing syntax error
and how it is same as string-2.

Because string literals are concatenated before syntactic and
semantic analysis.
The preprocessor will concatenate them.

Nit: The compiler will concatenate them. Preprocessors are
usually limited to translation phases 1 through 4. String
concatenation is translation phase 6 [cf 5.1.1.2.]

And the reason for this apparently pointless thing is that it lets you
construct string literals at compile time:

#define NAME "Nick"
char a[] = "Hello " NAME " and welcome"

Combined with the "stringizing" feature and you can can do even more.
 
N

Nick Keighley

Peter Nilsson said:
Ian Collins said:
    I was trying to understand some code and came across a
string literal definition as:
char a[]="abc""def";    // string-1
   I had seen eariler only string literal with characters
in a single pair of quotes(") e.g.
  char a[]="abcdef";     // string-2
Please clear why string-1 is not showing syntax error
and how it is same as string-2.
Because string literals are concatenated before syntactic and
semantic analysis.
Nit: The compiler will concatenate them. Preprocessors are
usually limited to translation phases 1 through 4. String
concatenation is translation phase 6 [cf 5.1.1.2.]

And the reason for this apparently pointless thing is that it lets you
construct string literals at compile time:

#define NAME "Nick"
char a[] = "Hello " NAME " and welcome"

Combined with the "stringizing" feature and you can can do even more.

and break up long strings over multiple lines
 
N

Nick

Kenneth Brody said:
Peter Nilsson said:
I was trying to understand some code and came across a
string literal definition as:
char a[]="abc""def"; // string-1
[...]
And the reason for this apparently pointless thing is that it lets you
construct string literals at compile time:

#define NAME "Nick"
char a[] = "Hello " NAME " and welcome"

Combined with the "stringizing" feature and you can can do even more.

You forgot about:

char foo[] = "This represents a"
" long string, which"
" won't fit on a"
" single line.";

I did indeed
or
char foo[] = "This is line 1.\n"
"And this is another line.";

Which is a huge improvement over:
char foo[] = "This is line 1.
And this is another line.";

Did C ever allow this. I've a hazy feeling some versions might have.
 
K

Keith Thompson

Nick said:
Kenneth Brody said:
I was trying to understand some code and came across a
string literal definition as:
char a[]="abc""def"; // string-1 [...]
And the reason for this apparently pointless thing is that it lets you
construct string literals at compile time:

#define NAME "Nick"
char a[] = "Hello " NAME " and welcome"

Combined with the "stringizing" feature and you can can do even more.

You forgot about:

char foo[] = "This represents a"
" long string, which"
" won't fit on a"
" single line.";

I did indeed
or
char foo[] = "This is line 1.\n"
"And this is another line.";

Which is a huge improvement over:
char foo[] = "This is line 1.
And this is another line.";

Did C ever allow this. I've a hazy feeling some versions might have.

No C standard ever allowed this, but I think many pre-ANSI compilers
did.

You could also write:

char foo[] = "This is line 1.\n\
And this is another line.";

But that's also quite ugly. Apart from depending critically on the
indentation of the second line (something that's easy to mess up while
maintaining the code), it can become a syntax error if there's any
whitespace after the \.
 

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
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top