473,836 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
+ 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 2625
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************ **********@i42g 2000cwa.googleg roups.com>
reader <re****@vp.plwr ote:
>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.ukwri tes:
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_Keit h) 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
2578
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
3316
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 time, and inline functions are all defined in headers. " Can someone explain to me why some of the const objects must be defined in the header file?
10
2799
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1879
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
8
4583
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
230
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
1932
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
1936
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 article/tutorial that can help me? Thanks a lot!
2
2365
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 compiler complains of function 'g' returning a
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10859
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9388
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7794
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6982
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4460
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.