Some Questions

F

fctk

hello,

i have some questions.

1) do constant expressions include string literals? for example, is
"hello, world" a constant expression?

2) int i = 0; is the equal sign the assignment operator or is it only a
symbol? (i think the last one is correct)

3) is an array a variable? for example:

int i = 0; /* `i' is a variable*/
char *cPtr; /* `cPtr' is a variable */
int a[10]; /* is `a' a variable? */

K&R (2ed) says (paragraph 5.3; page 99): "[...] the value of a variable
[...] of type array [...]".

so i think an array is a variable.

thanks
 
W

Walter Roberson

1) do constant expressions include string literals? for example, is
"hello, world" a constant expression?

Not generally. It is, however, an address constant, which can be
used in the context of initializers.
2) int i = 0; is the equal sign the assignment operator or is it only a
symbol? (i think the last one is correct)

That's a matter of semantics, but ask yourself whether the syntax
would permit (say) int i -= 5; to determine whether the syntax
permits assignment operators in general.

3) is an array a variable? for example:
int i = 0; /* `i' is a variable*/
char *cPtr; /* `cPtr' is a variable */
int a[10]; /* is `a' a variable? */

The C89 standard does not have variables: it has objects, some of
which are modifiable and some of which are not. Arrays are not
modifiable, but array elements are modifiable. In your above examples,
i, cPtr, and a are all "identifiers".
 
F

fctk

Walter Roberson ha scritto:
That's a matter of semantics, but ask yourself whether the syntax
would permit (say) int i -= 5; to determine whether the syntax
permits assignment operators in general.

the compiler does not permit: int i -= 5; so i guess the equal sign in a
variable definition is not an operator, but a sign (such as the
semicolon at the end of statements).
3) is an array a variable? for example:

int i = 0; /* `i' is a variable*/
char *cPtr; /* `cPtr' is a variable */
int a[10]; /* is `a' a variable? */


The C89 standard does not have variables: it has objects, some of
which are modifiable and some of which are not. Arrays are not
modifiable, but array elements are modifiable. In your above examples,
i, cPtr, and a are all "identifiers".

but we can agree that a "variable" is a modifiable object, while a
"constant" is a non-modifiable object. so an array is a constant, while
array elements are variables.

in my previous example, `i', `cPtr' and `a' are identifiers which refers
to (in order) a variable, a variable, a constant.

anyway if i compile the following little program:

int main(void) {
int a[10];
return 0;
}

i get the following warning:

test.c: In function `main':
test.c:2: warning: unused variable `a'

please notice the word "variable"...

i'm a bit confused.
 
W

Walter Roberson

Walter Roberson ha scritto:
anyway if i compile the following little program:
int main(void) {
int a[10];
return 0;
}
i get the following warning:
test.c: In function `main':
test.c:2: warning: unused variable `a'
please notice the word "variable"...
i'm a bit confused.


C compilers are not required to emit a message in that case, so
any message they do emit need not be semantically accurate.
but we can agree that a "variable" is a modifiable object, while a
"constant" is a non-modifiable object.

int main(void) {
const int i = 0;
return 0;
}

$ cc -fullwarn -o tc tc.c
cc-1174 cc: WARNING File = tc.c, Line = 2
The variable "i" was declared but never referenced.

const int i = 0;
^
$ gcc -Wall -ansi -std=c89 -o tc tc.c
tc.c: In function `main':
tc.c:2: warning: unused variable `i'


By your definition of variable, i is not a variable and hence
the diagnostics would have to be incorrect [in the context of your
definition.]

This suggests to me that your definition of "variable" is not
the same as is commonly used. But the C language doesn't care
what your definition of "variable" is, as it does not talk
about "variables".

A question for you: is a malloc()'d block of memory a "variable" ?
It is modifiable...
 
A

Abdo Haji-Ali

the compiler does not permit: int i -= 5; so i guess the equal sign in a
variable definition is not an operator, but a sign (such as the
semicolon at the end of statements).
I wouldn't depend on that, as you can override this "assaignment
operator"
while you can't override a semicolon
but we can agree that a "variable" is a modifiable object, while a
"constant" is a non-modifiable object. so an array is a constant, while
array elements are variables.
Depends on what you mean by modifiable, do you mean that it's size
can't be
changed? if this is what you meant, then you'll have to distiguish
between
arrays allocated on the heap (which can be changed in size, under some
limitations) and arrays allocated on the stack (which can't be changed
in
size)
i get the following warning:

test.c: In function `main':
test.c:2: warning: unused variable `a'

please notice the word "variable"...
Well even if you declare a constant "variable" the compiler would warn
that
a "variable" is declared but not used...
 
F

fctk

Walter Roberson ha scritto:
This suggests to me that your definition of "variable" is not
the same as is commonly used. But the C language doesn't care
what your definition of "variable" is, as it does not talk
about "variables".

A question for you: is a malloc()'d block of memory a "variable" ?
It is modifiable...

according to my (wrong) definition, a malloc()'s block of memory is a
variable.

according to C language, i guess it is not even an object, because an
object is "a location of memory which has a name", but malloc()'s blocks
of memory have not a name.

IMHO.
 
S

Skarmander

Walter said:
Not generally. It is, however, an address constant, which can be
used in the context of initializers.


That's a matter of semantics, but ask yourself whether the syntax
would permit (say) int i -= 5; to determine whether the syntax
permits assignment operators in general.
The "=" in an initializer cannot be an assignment operator because the
grammar does not permit assignment operators there. The production rule
includes a literal "=". Although "=" also happens to match an assignment
operator in another context, it's not the same token here. I wouldn't call
that semantics; it's still pure syntax.

When you say "semantics" you presumably refer to the fact that we often call
the symbol "=" the "assignment operator", confusing levels. That this is
easy to do doesn't make it a debatable issue, though.

S.
 
W

Walter Roberson

according to C language, i guess it is not even an object, because an
object is "a location of memory which has a name", but malloc()'s blocks
of memory have not a name.

C89 section 1.6:

Object -- a region of data storage in the execution environment,
the contents of which can represent values. Except for bit-fields,
objects are composed of contiguous sequences of one or more bytes,
the number, order, and encoding of which are either explicitly
specified or implementation-defined. When referenced, an object
may be interrpreted as having a particular type; see 3.2.2.1.


Notice no reference to having a name...
 
W

Walter Roberson

I wouldn't depend on that, as you can override this "assaignment
operator"
while you can't override a semicolon

I don't think I know what you mean there by "override" that assignment
operator ? Are you perchance thinking of C++'s ability to overload
operators?
Depends on what you mean by modifiable, do you mean that it's size
can't be
changed? if this is what you meant, then you'll have to distiguish
between
arrays allocated on the heap (which can be changed in size, under some
limitations) and arrays allocated on the stack (which can't be changed
in
size)

Whyever would you want to distinguish on that basis? C has no idea
what a "stack" or "heap" are. Objects in C are auto, static, or
dynamically allocated, but as far as C is concerned, stack or heap
or mmap or punch-tape is a petty implementation detail.
 
R

rayw

fctk said:
hello,

i have some questions.

1) do constant expressions include string literals? for example, is
"hello, world" a constant expression?

2) int i = 0; is the equal sign the assignment operator or is it only
a symbol? (i think the last one is correct)

3) is an array a variable? for example:

int i = 0; /* `i' is a variable*/
char *cPtr; /* `cPtr' is a variable */
int a[10]; /* is `a' a variable? */

K&R (2ed) says (paragraph 5.3; page 99): "[...] the value of a
variable [...] of type array [...]".

so i think an array is a variable.

thanks

hello,

i have some questions.

1) do constant expressions include string literals? for example, is
"hello, world" a constant expression?

2) int i = 0; is the equal sign the assignment operator or is it only
a symbol? (i think the last one is correct)

3) is an array a variable? for example:

int i = 0; /* `i' is a variable*/
char *cPtr; /* `cPtr' is a variable */
int a[10]; /* is `a' a variable? */

K&R (2ed) says (paragraph 5.3; page 99): "[...] the value of a
variable [...] of type array [...]".

so i think an array is a variable.

Here's what I think, sans not reading other's replies - ready for any
flak/corrections, and ready to learn :)

1.

int n = 0; // 0 is a constant expression

Likewise -

char * p1 = "boo"; // "boo" is a constant expression used to set 'p'. Not
sure if 'set' is right!

char p2[] = "boo"; // "boo" is a constant expression used to set 'p'. Not
sure if 'set' is right!

p1, as it's a variable, may point somewhere else though. p2 cannot however -
it's not a variable - it's a constant address see '3'.

So, for your '1', 'YES' - "hello, world" is a constant expression.

2.

int i = 0;

The '=' here you may like to think of as assignment - although IMHO it's
best to think of it as an 'initialization operator' - i is being
initialized, not assigned to.

int i;

i = 0; // now '=' is an assignment operator.

Don't know how much difference there is in C - but there's A LOT in C++

3.

An array name is a constant address - the address of the first element in
the array - see '1'

rayw
 
K

Keith Thompson

fctk said:
Walter Roberson ha scritto:

according to my (wrong) definition, a malloc()'s block of memory is a
variable.

according to C language, i guess it is not even an object, because an
object is "a location of memory which has a name", but malloc()'s
blocks of memory have not a name.

No, an object is a "region of data storage in the execution
environment, the contents of which can represent values" (C99
standard, section 3.14). A block of memory allocated by malloc() is
an object.

Where did the definition as "a location of memory which has a name"
come from?

The C standard doesn't define or use use the term "variable", at least
not formally, but it's a very common term in discussions about C (or
about programming in general). It's not clear whether an unnamed
object (such as the unnamed block allocated by malloc()) should be
considered a "variable"; likewise for an object that's a component of
a larger object.

Personally, I use the term "variable" only informally, and only to
refer to a declared named (and non-const) object. The question of
whether other objects qualify as "variables" is not a particularly
interesting one; if I avoid using the word in such cases, I don't have
to decide one way or the other, and I avoid arguments with people who
will inevitably think I'm misusing the word.
 
W

Walter Roberson

It's a matter of syntax, not semantics.

OED indicates:

Semantic: B. n. pl.
1.a = SEMASIOLOGY. Als, (the study or analysis of) the relationships
between linguistic symbols and their meanings. Const. as sing. and pl.
Now the usual word in this sense.


The equal sign is a "linguistic symbol". The -meaning- of that linguistic
symbol in context is semantics, not syntax. Syntax tells you that the
equal sign must occur there, but not what it denotes.
 
A

Abdo Haji-Ali

I don't think I know what you mean there by "override" that assignment
operator ? Are you perchance thinking of C++'s ability to overload
operators?
Yep! Sorry I keep forgetting that this is a C newsgroup not a C++
one...
Whyever would you want to distinguish on that basis? C has no idea
what a "stack" or "heap" are. Objects in C are auto, static, or
dynamically allocated, but as far as C is concerned, stack or heap
or mmap or punch-tape is a petty implementation detail.
Again, sorry. However, same thing applies for dynamically allocated
arrays vs. static arrays...

Abdo Haji-Ali
Programmer
In|Framez
 
W

Walter Roberson

Whyever would you want to distinguish on that basis? C has no idea
what a "stack" or "heap" are. Objects in C are auto, static, or
dynamically allocated, but as far as C is concerned, stack or heap
or mmap or punch-tape is a petty implementation detail.
[/QUOTE]
Again, sorry. However, same thing applies for dynamically allocated
arrays vs. static arrays...

C does not have dynamically allocated arrays. C has dynamically
allocated objects, the bytes of which may be interpreted as if an
array (or structure or simple non-composite value.) If one has
a dynamically allocated object in C which has been interpreted as
an array, the array cannot be resized: the object can be resized
and the resulting object interpreted as a different array
with the same values as before [up to the minimum of the old and
new sizes.]

To make the difference slightly clearer: one could allocate [sufficient]
dynamic memory and use it as if an array of 10 int values. One could
then realloc() the object and interpret that as a structure
the first component of which is an array of 10 int values, but then
followed by (say) a union, then a composite structure, then another
array... If the semantics of realloc() applied to "a dynamically
allocated array" was that it "resized the array" then you would not
be able to do that: those semantics would require that the expanded
space only be used for additional array elements of the same type.

When you malloc() or calloc() and convert the resulting void* into
a different pointer type, you impose a form on the dynamic memory.
When you realloc(), you toss away knowledge of that form and you
get back a blob of memory -- which you can then impose a different form
on, and you can access the previously-stored values provided that
the new form is "compatable" with the old in the accessed locations.
Sure, the new form might happen to be a larger version of the
array you had before, but the semantics don't require that at all.
 
P

pete

Walter said:
fctk <-> wrote:

That's a matter of semantics, but ask yourself whether the syntax
would permit (say) int i -= 5; to determine whether the syntax
permits assignment operators in general.

I think it helps to consider that
int i = 0;
is shorthand for
int i = {0};
and that {0} is not an expression.

Those are declarations, not statements.
And those are initializations, not assignments.
 
M

Mark McIntyre

but we can agree that a "variable" is a modifiable object,

This isn't a variable according to my understanding - any non-constant
is a variable, eg
const int x;

The only actual constants in C are defined at compile time.
while a
"constant" is a non-modifiable object. so an array is a constant, while
array elements are variables.

No.
Mark McIntyre
 
S

Skarmander

Walter said:
OED indicates:

Semantic: B. n. pl.
1.a = SEMASIOLOGY. Als, (the study or analysis of) the relationships
between linguistic symbols and their meanings. Const. as sing. and pl.
Now the usual word in this sense.


The equal sign is a "linguistic symbol".

I follow you so far.
The -meaning- of that linguistic symbol in context is semantics, not
syntax. Syntax tells you that the equal sign must occur there, but not
what it denotes.

Yes, but the standard defines the *syntactical* category
"assignment-operator", and the elements of that category cannot occur in an
initializer. What "=" denotes in an initializer is irrelevant (although it
arguably does not denote *anything*): whatever it is, it's not an assignment
operator. To argue otherwise is to argue that the grammar does not define
what (syntactically) an assignment operator is, which is a questionable
position to say the least.

The question was not "what does '=' mean in an initializer" but "is '=' an
assignment operator in an initializer", and the answer to the latter
question is no, because *syntactically* assignment operators cannot occur in
initializers (at the top level; they can of course occur in the initializing
expression).

"What is assignment" is a question of semantics. "Is '=' an assignment
operator here" is a question of syntax. Alternative views on the distinction
between these are possible, but they are unlikely to have the same broad
applicability or offer more understanding.

This is orthogonal to general philosophical discussions about where, if
anywhere, the line between syntax and semantics can or should be drawn,
which is far more off-topic than what the current discussion is already
edging towards.

S.
 
P

pete

rayw said:
char p2[] = "boo";
// "boo" is a constant expression used to set 'p'. Not
sure if 'set' is right!

"boo" is shorthand for {'b', 'o', 'o', '\0'}
p1, as it's a variable, may point somewhere else though.
p2 cannot however it's not a variable
it's a constant address see '3'.

p2 is an lvalue expression of type array of four char.
sizeof p2 equals 4.
3.

An array name is a constant address
- the address of the first element in
the array - see '1'

Only when the array has static duration,
does it's conversion to a pointer
yield an address constant.
 
F

fctk

Keith Thompson ha scritto:
No, an object is a "region of data storage in the execution
environment, the contents of which can represent values" (C99
standard, section 3.14). A block of memory allocated by malloc() is
an object.

Where did the definition as "a location of memory which has a name"
come from?

K&R (2ed) - A5 (p197): "An object is a named region of storage".
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top