By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Is "hello, world" a constant

P: n/a
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the
"hello, world" thing using the name "string-constant". But `ISO/IEC
9899:TC2' does not include this kind of thing in section `A.1.5
Constants'.

May 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ed
On 19 May 2006 12:02:45 -0700
"lovecreatesbeauty" <lo***************@gmail.com> wrote:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the
"hello, world" thing using the name "string-constant". But `ISO/IEC
9899:TC2' does not include this kind of thing in section `A.1.5
Constants'.


It might have helped if you quoted those sections. "Hello world" is a
literal. It is immutable. You cannot change this what-so-ever, unless
it is given as via a copy function:

char s[20] = "Hello world";

This allocated memory for s, straight after the characters are copied
in.

--
Regards, Ed :: http://www.usenix.org.uk
just another perl person
:%s/Open Source/Free Software/g :: Free DNS available

May 19 '06 #2

P: n/a
"lovecreatesbeauty" <lo***************@gmail.com> writes:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the
"hello, world" thing using the name "string-constant". But `ISO/IEC
9899:TC2' does not include this kind of thing in section `A.1.5
Constants'.


You'll find it in the following section, "String literals".

In the C99 grammar, we have:

token:
keyword
identifier
constant
string-literal
punctuator

constant:
integer-constant
floating-constant
enumeration-constant
character-constant

primary-expression:
identifier
constant
string-literal
( expression )

"primary-expression" and "token" are the only productions that refers
to "constant".

Making a "string-literal" a separate kind of token, rather than
another kind of "constant", was an arbitrary choice. The following
would describe exactly the same language:

token:
keyword
identifier
constant
/* remove string-literal */
punctuator

constant:
integer-constant
floating-constant
enumeration-constant
character-constant
string-literal /* add this */

primary-expression:
identifier
constant
/* remove string-literal */
( expression )

The wording in K&R2 and H&S5 is just a slightly different way of
describing exactly the same thing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 19 '06 #3

P: n/a
On Fri, 19 May 2006 19:31:45 GMT, ed <ed@noreply.com> wrote in
comp.lang.c:
On 19 May 2006 12:02:45 -0700
"lovecreatesbeauty" <lo***************@gmail.com> wrote:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the
"hello, world" thing using the name "string-constant". But `ISO/IEC
9899:TC2' does not include this kind of thing in section `A.1.5
Constants'.
It might have helped if you quoted those sections. "Hello world" is a
literal. It is immutable. You cannot change this what-so-ever, unless


Specifically, a string literal.

It may or may not be immutable. It has the type "array of char" in C,
and specifically does not have the type "array of const char".
Attempting to modify a string literal produces undefined behavior not
because it has the type "array of const char", but merely because the
C standard specifically states that it does.
it is given as via a copy function:
I have no idea what the above phrase means. There is no function
involved in your code snippet below.
char s[20] = "Hello world";
This is a declaration of an object with initialization, there is no
function involved here.
This allocated memory for s, straight after the characters are copied
in.


This causes the array 's' to be initialized at its creation with the
11 characters inside the quoted string literal followed by 9 '\0'
characters. How this initialization is performed is up to the
implementation. There need not be a "copy function" involved.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.