Is "hello, world" a constant

Discussion in 'C Programming' started by lovecreatesbeauty, May 19, 2006.

  1. 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'.
     
    lovecreatesbeauty, May 19, 2006
    #1
    1. Advertising

  2. lovecreatesbeauty

    ed Guest

    On 19 May 2006 12:02:45 -0700
    "lovecreatesbeauty" <> 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
     
    ed, May 19, 2006
    #2
    1. Advertising

  3. "lovecreatesbeauty" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 19, 2006
    #3
  4. lovecreatesbeauty

    Jack Klein Guest

    On Fri, 19 May 2006 19:31:45 GMT, ed <> wrote in
    comp.lang.c:

    > On 19 May 2006 12:02:45 -0700
    > "lovecreatesbeauty" <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2Vhbg==?=

    Server Error in '/Hello World' Application

    =?Utf-8?B?U2Vhbg==?=, Jul 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    366
    Ben Lucas
    Jul 20, 2004
  2. =?Utf-8?B?cmFuZHkgY29sbGlucw==?=

    ASP.NET 2.0: "hello world" page fails with "Failed to map the path

    =?Utf-8?B?cmFuZHkgY29sbGlucw==?=, Apr 15, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    5,599
    eagle1140
    Apr 13, 2011
  3. vijay
    Replies:
    8
    Views:
    741
  4. Roy
    Replies:
    6
    Views:
    673
    Roedy Green
    Jan 7, 2008
  5. Larry
    Replies:
    27
    Views:
    469
    Michele Dondi
    Jan 25, 2005
Loading...

Share This Page