468,525 Members | 2,304 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

String, String literal ? Could anyone explain to me ?

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
Nov 13 '05 #1
7 6703

"herrcho" <he*********@kornet.net> wrote in message
news:bk*********@news1.kornet.net...
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
Nov 13 '05 #2

"Mike Wahler" <mk******@mkwahler.net> schrieb im Newsbeitrag
news:Mg*****************@newsread3.news.pas.earthl ink.net...

"herrcho" <he*********@kornet.net> wrote in message
news:bk*********@news1.kornet.net...
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
Nov 13 '05 #3
"Robert Stankowic" <pc******@netway.at> wrote:
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
Nov 13 '05 #4
Robert Stankowic wrote:

"Mike Wahler" <mk******@mkwahler.net> schrieb im Newsbeitrag
news:Mg*****************@newsread3.news.pas.earthl ink.net...

"herrcho" <he*********@kornet.net> wrote in message
news:bk*********@news1.kornet.net...
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.

--
pete
Nov 13 '05 #5
On Thu, 25 Sep 2003 05:44:12 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:

"herrcho" <he*********@kornet.net> wrote in message
news:bk*********@news1.kornet.net...
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').


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
Nov 13 '05 #6
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:eg********************************@4ax.com...
On Thu, 25 Sep 2003 05:44:12 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:

"herrcho" <he*********@kornet.net> wrote in message
news:bk*********@news1.kornet.net...
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').


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
Nov 13 '05 #7
On Fri, 26 Sep 2003 06:26:30 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:

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


Now you've gone and broken my heart! :(

--
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
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by pvinodhkumar | last post: by
14 posts views Thread by Christopher Benson-Manica | last post: by
16 posts views Thread by Don Starr | last post: by
17 posts views Thread by Olivier Bellemare | last post: by
5 posts views Thread by John Baro | last post: by
35 posts views Thread by Smithers | last post: by
2 posts views Thread by h03Ein | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.