Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > (C99) Does "const int x=5;" make x a "constant expression"?

Reply
Thread Tools

(C99) Does "const int x=5;" make x a "constant expression"?

 
 
jaime
Guest
Posts: n/a
 
      06-16-2007
Hi again all.

Given the line:
const int x=5;
Can I then use "x" as a constant expression? (By "constant expression", I
mean "constant expression" as defined in the C99 standard)

I've been searching google for 2 days now trying to answer this myself,
and I'm just getting more and more confused (some things I read make me
think "yes", while some things I read make me think "no").

I have many questions I'd like to ask on this topic, but rather than bore
you all rigid with the results of all of my research (points for and
against), I thought I'd just try this short question first.

Also, could anyone answering please give me an idea of how I can infer the
answer by reading the "Standard" (which I _think_ is currently ISO/IEC
9899:TC2).

So, baffled once again, I humbly seek wise words from the council of
elders...

Ta, Jaime
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      06-16-2007
jaime wrote:
>
> Given the line:
> const int x=5;
> Can I then use "x" as a constant expression? (By "constant
> expression", I mean "constant expression" as defined in the C99
> standard)


No. It is a constant object, not expression. I.E. it cannot be
altered.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      06-16-2007
jaime wrote:
> Hi again all.
>
> Given the line:
> const int x=5;
> Can I then use "x" as a constant expression? (By "constant expression", I
> mean "constant expression" as defined in the C99 standard)
>
> I've been searching google for 2 days now trying to answer this myself,
> and I'm just getting more and more confused (some things I read make me
> think "yes", while some things I read make me think "no").
>
> I have many questions I'd like to ask on this topic, but rather than bore
> you all rigid with the results of all of my research (points for and
> against), I thought I'd just try this short question first.
>
> Also, could anyone answering please give me an idea of how I can infer the
> answer by reading the "Standard" (which I _think_ is currently ISO/IEC
> 9899:TC2).
>
> So, baffled once again, I humbly seek wise words from the council of
> elders...
>
> Ta, Jaime


No. const != constant in C. In your example..
const int x = 5;
...x is const and 5 is constant. You can't use x where you need a constant.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-16-2007
jaime <> writes:

> Hi again all.
>
> Given the line:
> const int x=5;
> Can I then use "x" as a constant expression? (By "constant expression", I
> mean "constant expression" as defined in the C99 standard)


Short: no.

Medium: not if you want portable code.

Long: read all of section 6.6 of the standard. The trouble is it says
what you *can* have. The value of a variable, const, or otherwise is
not one of these.

Section 6.6 does permit implementations to allow other forms, so you
might be able to do it, but your code will not be portable. Such am
implementation would be violating the spirit in which const was
introduced (to signify a read-only, run-time object).

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-16-2007
jaime <> writes:
> Hi again all.
>
> Given the line:
> const int x=5;
> Can I then use "x" as a constant expression? (By "constant expression", I
> mean "constant expression" as defined in the C99 standard)
>
> I've been searching google for 2 days now trying to answer this myself,
> and I'm just getting more and more confused (some things I read make me
> think "yes", while some things I read make me think "no").

[...]

No, "const" in C really means "read-only", not "constant".

Your Google search probably confused you because <OT>x is a constant
expression in C++</OT>.

--
Keith Thompson (The_Other_Keith) kst- <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does "template<typename T> ... function<T(int)>" not match "int(&)(int)" implicitly? Steve Hicks C++ 2 09-28-2009 05:24 PM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57