473,239 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

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 7044

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
14
by: Christopher Benson-Manica | last post by:
I'm trying to check whether a string is all digits. This part is easy: function allDigits( str ) { var foo=str.split( '' ); // better than charAt()? for( var idx=0; idx < foo.length; idx++ ) {...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
2
by: h03Ein | last post by:
Hi! during my search on tokens in ANSI C I have found following specification for string literals based on regular expression in site http://www.lysator.liu.se/c/ANSI-C-grammar-l.html :...
2
by: slimewizard | last post by:
Hello there! I'm writing a 'hang man' program as a bit of a practice exercise and I've ran into a bit of a problem; I just need a little advise if anyone would be so kind. My apologies if this...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.