Const expression

S

sarathy

Hi,
Can anyone just help me with what exactly is constant
expression. I read the section on Constant expression in K&R2. i am not
fully clear with it.
 
M

MQ

sarathy said:
Hi,
Can anyone just help me with what exactly is constant
expression. I read the section on Constant expression in K&R2. i am not
fully clear with it.

A constant expression is one that will always evaluate to the same
value every time. For example, the number 3 and the string "Hello,
world!" are constant expressions. Compare that to the expression x+2,
which is not constant, as it depends on the value of x

MQ
 
C

Chris Dollin

MQ said:
A constant expression is one that will always evaluate to the same
value every time.

No, that's not the definition. It's /true/, but it's not the definition.
For example, the number 3 and the string "Hello,
world!" are constant expressions.

Strings are not constant expressions (except in initialisers). For example,
`int spoo["1"[0]];` isn't legal. Er, isn't legal in C90. OK, try
`enum { SPOO = "1"[0] };`.
Compare that to the expression x+2,
which is not constant, as it depends on the value of x

`x + 2` is a constant expression if `x` is a constant expression, which it can
be if `x` is a enum constant.

Roughly speaking, a constant expression is an expression with no possible
updates (no assignment, increment, decrement), no function calls (not even
for built-in functions) and no comma-expressions (which would be harmless
but pointless). [There's an exception for things inside `sizeof`, which aren't
[C90] evaluated anyway]. The leaf operands must be literals or enum constants
or the addresses of static store [that last in limited circumstances].
 
R

reader

Hi,
Can anyone just help me with what exactly is constant
expression. I read the section on Constant expression in K&R2. i am not
fully clear with it.

Basically it's - read only.
(It can change e.g. some register value etc)
 
C

Chris Dollin

reader said:
Basically it's - read only.

Basically, it's not.

(Yes, it can't do any updates. No, that's nowhere near a sufficient
definition.)
(It can change e.g. some register value etc)

If you mean register variables, no, it can't. If you mean machine-level
registers, evaluation a constant expression /could/ change such off-topic
entities, true; but the /program/ can't (legally) see them, and it's
rather likely to be the /compiler/ where that evaluation happens, not
the executing program.
 
R

reader

Basically, it's not.

Well I would still insist that generally it means READ-ONLY.
Especially that you so nicelly explained that in the second part of
your post :)

And yes. I meant machine level registers, like in embedded
programming where const and volatile is used very often.

It has of course some optimization implications and other stuff,
but for someone who's starting with K&R it's enough I suppose
to say - read only - since it's not a constant.
 
F

Flash Gordon

reader said:
Well I would still insist that generally it means READ-ONLY.
Especially that you so nicelly explained that in the second part of
your post :)

And yes. I meant machine level registers, like in embedded
programming where const and volatile is used very often.

It has of course some optimization implications and other stuff,
but for someone who's starting with K&R it's enough I suppose
to say - read only - since it's not a constant.

Basically, you have completely miss-read what the OP was asking about.
The OP was asking about constant expressions, *not* the const keyword.
int i = 5;
The 5 above is a constant expression, the word const does not appear
anywhere. 5+9234 is also a constant expression.
 
C

Chris Dollin

reader said:
Well I would still insist that generally it means READ-ONLY.

If you mean by "means" that the expression doesn't update any
variables, yes.

If you mean that being read-only means that it's a C constant-
expression, no.

"generally it means READ-ONLY" to mean reads as saying that this
is the primary characteristic of a constant-expression. It isn't.
The primary characteristic is that, since it is composed of
constants (including enumed names) and update-free operations,
it can be (and is) evaluated by the compiler.

Consider the following

int example( int i )
{ return i + 1; }

The expression `i + 1` is read-only, but is not a constant expression.
And yes. I meant machine level registers, like in embedded
programming where const and volatile is used very often.

Those (machine registers) are off-topic here.

Note that `const` doesn't have anything to do with constant-expressions.
It has of course some optimization implications and other stuff,
but for someone who's starting with K&R it's enough I suppose
to say - read only - since it's not a constant.

Constant expressions /are/ constant, except for the ones involving
the addresses of static objects, which are constant from the
program's POV even if perhaps the addresses are different from
run to run.

If you must simplify, a less misleading description than yours,
I think, is "an expression involving only constants".
 
C

Chris Torek

Well I would still insist that generally it means READ-ONLY.
Especially that you so nicelly explained that in the second part of
your post :)

There is a problem here: the word "it" refers to -- what?

Here is the original subject line:

Const expression

(complete with uppercase "C").

Here is the original *text* of the original article:

Can anyone just help me with what exactly is constant
expression. I read the section on Constant expression
in K&R2. i am not fully clear with it.

The thing that means "read-only", in C, is the type qualifier
"const".

A constant-expression, in C, has nothing to do with the "const"
keyword. (C++ is different, as usual. :) )
And yes. I meant machine level registers, like in embedded
programming where const and volatile is used very often.

So, clearly, when you used the word "it" above, you meant the
"const" qualifier, as in "const and volatile" (and in C99, the
new "restrict" qualifier).

Unfortunately, I believe the original poster was not asking about
"const", but rather about constant-expressions.

For a complete definition of constant-expression, one must refer
back to the relevant standard (C89 or C99; there are some minor
differences between the two). The collection of "things that are
constant-expressions" is somewhat peculiar -- for instance, after:

enum { THREE = 3 };
const int FOUR = 4;

the following *are* constant-expressions:

3
THREE
4

but:

FOUR

is not a constant-expression. Moreover, most operators, applied
to something that is a constant-expression, produce a new
constant-expression with the obvious value:

THREE + 4

is a constant-expression and is just 7. Yet the comma operator,
which evaluates its left-side expression and then discards the
value and then evaluates its right-side expression and produces
that as its value, is never a constant-expression, even if both
operands are constant:

1, 2 /* not a constant-expression */

The latter actually avoids some common errors:

int M[5, 7]; /* oops, programmer "meant" int M[5][7] */

int f(void) { ... }

should generally draw a diagnostic, because (5, 7) is not a
constant-expression. There is a somewhat odd sentence in the
Standards:

An implementation may accept other forms of constant expressions.

Exactly what this means is not clear to me, since implementations
*can* *always* do stuff "beyond the standard" as long as they
provide whatever diagnostics are required by the standard. That
is, after printing something like:

warning: 5,7 is not a Standard-conforming constant-expression

any C compiler can go on and treat it as if you had just written
7. So what is the point of this extra sentence?
 
K

Keith Thompson

Flash Gordon said:
Basically, you have completely miss-read what the OP was asking
about. The OP was asking about constant expressions, *not* the const
keyword.
int i = 5;
The 5 above is a constant expression, the word const does not appear
anywhere. 5+9234 is also a constant expression.

Yes, it's very important to remember that "const" and "constant" (as
in "constant expression") mean very different things in C. The use of
"Const" in the subject header of an article asking about constant
expressions just adds to the confusion. (That's not the OP's fault;
it's natural to assume that "const" means "constant".)

For a definitive definition of "constant expressions", download
n1124.pdf and read section 6.6, "Constant expressions". It's just two
pages long.

Briefly: "A constant expression can be evaluated during translation
rather than runtime, and accordingly may be used in any place that a
constant may be." (Note that a "constant" is what some other
languages call a "literal".) But there's more to it than that; not
everything that can be evaluated during translation is actually a
constant expression.
 

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
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top