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

J

jaime

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 :)
 
C

CBFalconer

jaime said:
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.
 
J

Joe Wright

jaime said:
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.
 
B

Ben Bacarisse

jaime said:
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).
 
K

Keith Thompson

jaime said:
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>.
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top