Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const in C

0 views
Skip to first unread message

Chander Bhardwaj

unread,
Oct 13, 2009, 10:08:14 AM10/13/09
to
Why const in C language is not a compile time constant ?

Regards,
Chander.

Morris Keesan

unread,
Oct 13, 2009, 10:30:04 AM10/13/09
to
On Tue, 13 Oct 2009 10:08:14 -0400, Chander Bhardwaj
<tinkubhar...@yahoo.co.in> wrote:

> Why const in C language is not a compile time constant ?

void foo(int bar)
{
const int xyzzy = bar;
/* etc. */
}


--
Morris Keesan -- mke...@post.harvard.edu

mohangupta13

unread,
Oct 14, 2009, 1:55:33 AM10/14/09
to
On Oct 13, 7:08 am, Chander Bhardwaj <tinkubhardwaj_1...@yahoo.co.in>
wrote:

> Why const in C language is not a compile time constant ?
as far as i think , its because when the standard was defined they
might not have given consideration to it, the C++ committee did
actually defined const as compile time constant ..
I believe they should make so in any future revision of C.
Regards
Mohan

>
> Regards,
> Chander.

Keith Thompson

unread,
Oct 14, 2009, 2:20:21 AM10/14/09
to

It's not possible to make "const" mean "compile-time constant" in all
cases. For example, this is (and should continue to be) perfectly
legal:

const int r = rand();

I think it would be possible to make the name of a const-qualified
object a constant expression if the initial value is a constant
expression; I don't believe it would break any existing valid code.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Tech07

unread,
Oct 16, 2009, 11:05:32 PM10/16/09
to
Keith Thompson wrote:
> mohangupta13 <mohang...@gmail.com> writes:
>> On Oct 13, 7:08 am, Chander Bhardwaj <tinkubhardwaj_1...@yahoo.co.in>
>> wrote:
>>> Why const in C language is not a compile time constant ?
>> as far as i think , its because when the standard was defined they
>> might not have given consideration to it, the C++ committee did
>> actually defined const as compile time constant ..
>> I believe they should make so in any future revision of C.
>
> It's not possible to make "const" mean "compile-time constant" in all
> cases. For example, this is (and should continue to be) perfectly
> legal:
>
> const int r = rand();
>
> I think it would be possible to make the name of a const-qualified
> object a constant expression if the initial value is a constant
> expression; I don't believe it would break any existing valid code.

"const" seems to be "a big can of worms". Can someone list the issues? It
almost seems like "double the code" and "half the productivity" kind of
thing. I haven't yet studied the issue, but that's the feeling I get from
language groups is that "const" is hardly a benign introduction into a
language. It looks like a key problem to be solved.


Joachim Schmitz

unread,
Oct 17, 2009, 7:26:10 AM10/17/09
to
Tech07 wrote:
<snip>

> "const" seems to be "a big can of worms".

Not too bad: "WORM: Write Once, Read Many"

Bye, Jojo

0 new messages