Is "hello, world" a constant

L

lovecreatesbeauty

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'.
 
E

ed

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.
 
K

Keith Thompson

lovecreatesbeauty said:
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.
 
J

Jack Klein

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top