473,473 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Const expression

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.

Jul 19 '06 #1
9 2595
MQ

sarathy wrote:
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

Jul 19 '06 #2
MQ wrote:
>
sarathy wrote:
>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.
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].

--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jul 19 '06 #3
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)

Jul 19 '06 #4
reader wrote:
>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.
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.

--
Chris "seeker" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jul 19 '06 #5
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.

Jul 19 '06 #6
reader wrote:
>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.
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 19 '06 #7
reader wrote:
>Basically, it's not.

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

--
Chris "enough slack round 'constant'" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Jul 19 '06 #8
In article <11**********************@i42g2000cwa.googlegroups .com>
reader <re****@vp.plwrote:
>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?
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 19 '06 #9
Flash Gordon <sp**@flash-gordon.me.ukwrites:
reader wrote:
>>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.

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Jul 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: devdatta_clc | last post by:
Hi C experts I've a bunch of questions. Consider this simplified piece of code. const int a = 10; int main () { static int b = a;
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
7
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
16
by: arnuld | last post by:
I have declared an int as const but compiler still says that it is not a const: include <stdio.h> #include <stdlib.h> int main() { const int MAXSIZE = 100;
7
by: Luna Moon | last post by:
Hi all, I just couldn't get myself clear about the usage of "const" in front of and/or behind variables, pointers, classes, objects and functions... It's too confusing... any good clear...
2
by: casul | last post by:
Hi All, Given the following code : char const & identity( char const &c ) { return c; } char const & f( ) { return identity('A'); } char const & g( ) { return 'A'; } Of course, the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.