Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is "hello, world" a constant

Reply
Thread Tools

Is "hello, world" a constant

 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      05-19-2006
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'.

 
Reply With Quote
 
 
 
 
ed
Guest
Posts: n/a
 
      05-19-2006
On 19 May 2006 12:02:45 -0700
"lovecreatesbeauty" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-19-2006
"lovecreatesbeauty" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-21-2006
On Fri, 19 May 2006 19:31:45 GMT, ed <(E-Mail Removed)> wrote in
comp.lang.c:

> On 19 May 2006 12:02:45 -0700
> "lovecreatesbeauty" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments