473,591 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7066

"herrcho" <he*********@ko rnet.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******@mkwah ler.net> schrieb im Newsbeitrag
news:Mg******** *********@newsr ead3.news.pas.e arthlink.net...

"herrcho" <he*********@ko rnet.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******@netwa y.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******@mkwah ler.net> schrieb im Newsbeitrag
news:Mg******** *********@newsr ead3.news.pas.e arthlink.net...

"herrcho" <he*********@ko rnet.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******@mkwah ler.net> wrote in comp.lang.c:

"herrcho" <he*********@ko rnet.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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:eg******** *************** *********@4ax.c om...
On Thu, 25 Sep 2003 05:44:12 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:

"herrcho" <he*********@ko rnet.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******@mkwah ler.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.l earn.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
2191
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
12324
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++ ) { if( !isDigit(foo) ) { return false; } }
16
17396
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
17
14328
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, int longueur) { char *resultat = " "; char *temporaire = " "; int nbr;
5
3014
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 "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0" I want this to be @"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0" to treat the escape characters as literals
35
3648
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 = string.Empty; // do this string myString; // do not do this Questions 1. Is that a good rule? 2. If so, why? If not, why not?
11
3047
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 spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
2
2772
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 : L?\"(\\.|)*\" which L stands for . can anyone explain what does it mean ? I know regex but I can't understand this specification. why L? . does it mean following input is correct: s"\a" for string literals. or what's exactly (\\.|) means. and so on......
2
3195
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 has been covered here before; I've tried googling this but I don't know how to word it so my results have been quite unhelpful. I'll try my best to explain what I'm trying to do through example. (Sorry if this is long winded :/) I wrote a...
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.