String, String literal ? Could anyone explain to me ?

H

herrcho

i'm in the course of learning C, and found these two words "string,
string literal" confusing me..

I'd like to know the difference between them.. Thank you
 
M

Mike Wahler

herrcho said:
i'm in the course of learning C, and found these two words "string,
string literal" confusing me..

I'd like to know the difference between them.. Thank you

In C, a 'string' is an array of characters, the
last of which has a value of zero. A string can
have automatic, static, or allocated duration.
It can be defined to be modifiable or not
(see 'const').

A 'string literal' can appear in source code by
enclosing a character sequence in double quotes as in:

"Hello"

A 'string literal' represents a nonmodifiable string
in your program's memory space. (It occupies one more
character than those expressed between the quotes --
an implied terminator character ('\0') ). So the
string literal "Hello" occupies six bytes.

char s[20]; /* an array of twenty uninitalized characters */
strcpy(s, "Hello"); /* copy the characters of the string
literal to the array 's' ('strcpy()'
automatically adds the '\0' terminator) */

/* now the array 's' contains a string. (Note that if
a terminator character ('\0') is not an element of
the array, then it's not a string */


-Mike
 
R

Robert Stankowic

Mike Wahler said:
herrcho said:
i'm in the course of learning C, and found these two words "string,
[....]

A 'string literal' can appear in source code by
enclosing a character sequence in double quotes as in:

"Hello"

A 'string literal' represents a nonmodifiable string
in your program's memory space. (It occupies one more
character than those expressed between the quotes --
an implied terminator character ('\0') ). So the
string literal "Hello" occupies six bytes.

I just wonder if an implementation is at all required to store a string
literal.
The statement
char foo[] = "Hello";
as well as
char *bar = "Hello";
char baz = bar[1];

for example can be executed using assembly instructions with immidiate
operands

Of course this would be a pretty strange implementation. The question is
just "does the standard _require_ string literals to be stored somewhere?"
Robert
 
R

Richard Bos

Robert Stankowic said:
I just wonder if an implementation is at all required to store a string
literal.

An implementation is required to do nothing at all, as long as the
effect is the same.
The statement
char foo[] = "Hello";
as well as
char *bar = "Hello";
char baz = bar[1];

for example can be executed using assembly instructions with immidiate
operands

Yup. And is allowed to be. In most cases this optimisation won't be
worth the trouble, but it's legal, all right.

Richard
 
P

pete

Robert said:
Mike Wahler said:
herrcho said:
i'm in the course of learning C, and found these two words "string,
[....]

A 'string literal' can appear in source code by
enclosing a character sequence in double quotes as in:

"Hello"

A 'string literal' represents a nonmodifiable string
in your program's memory space. (It occupies one more
character than those expressed between the quotes --
an implied terminator character ('\0') ). So the
string literal "Hello" occupies six bytes.

I just wonder if an implementation is at all required to store a string
literal.
The statement
char foo[] = "Hello";
as well as
char *bar = "Hello";

When initializing in an array of char declaration, as above,
the string literal's presence in the code,
does not imply that there is another object besides foo.

However, the string literal is
"the name of an anonymous object" (how's that for an oxymoron?)
in the pointer assignment.
 
J

Jack Klein

In C, a 'string' is an array of characters, the
last of which has a value of zero. A string can
have automatic, static, or allocated duration.
It can be defined to be modifiable or not
(see 'const').

I have to nit-pick this one. Consider:

char ca [20] = "Hello";
ca [19] = '!';

Now ca is an array of characters, the last of which most specifically
does not have a value of 0. Yet ca is a string.

From 7.1.1 of C99:

"A string is a contiguous sequence of characters terminated by and
including the first null character."

An array of characters may contain a string, as in my example, and not
meet the definition of a string you posted.
A 'string literal' can appear in source code by
enclosing a character sequence in double quotes as in:

"Hello"

A 'string literal' represents a nonmodifiable string
in your program's memory space. (It occupies one more
character than those expressed between the quotes --
an implied terminator character ('\0') ). So the
string literal "Hello" occupies six bytes.

char s[20]; /* an array of twenty uninitalized characters */
strcpy(s, "Hello"); /* copy the characters of the string
literal to the array 's' ('strcpy()'
automatically adds the '\0' terminator) */

/* now the array 's' contains a string. (Note that if
a terminator character ('\0') is not an element of
the array, then it's not a string */

BTW, usenet RFCs specify that a signature be separated from the body
of a message by a line consisting of the three characters "-- ".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
M

Mike Wahler

Jack Klein said:
In C, a 'string' is an array of characters, the
last of which has a value of zero. A string can
have automatic, static, or allocated duration.
It can be defined to be modifiable or not
(see 'const').

I have to nit-pick this one. Consider:

char ca [20] = "Hello";
ca [19] = '!';

Now ca is an array of characters, the last of which most specifically
does not have a value of 0. Yet ca is a string.

From 7.1.1 of C99:

"A string is a contiguous sequence of characters terminated by and
including the first null character."

An array of characters may contain a string, as in my example, and not
meet the definition of a string you posted.

Yes, that's what I meant. You're just picking on me
for fun now, huh? Just kidding. :) My description
was indeed sloppy. Thanks for 'cleaning it up'.


[snip]
BTW, usenet RFCs specify that a signature be separated from the body
of a message by a line consisting of the three characters "-- ".

IMO it's not a 'signature' in the sense you're using. It's
just part of my message body. Sometimes I put "Love," before
it, but I don't know you folks that well. :)

-Mike
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top