473,473 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constants not constant?

How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?

Nov 14 '05 #1
11 5179
ge*****@gmail.com writes:
How come this is illegal? const int N = 10;
double a[ N ]; I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?


N is a variable whose value is set at run-time.
Hence the size of 'a' cannot be known at compile time.

--
Chris.
Nov 14 '05 #2
gene...@gmail.com wrote:
How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any
function". What's the reason behind this syntax rule?


In C, const does not mean constant, it means read only.

C99 supports variable length arrays, but only with automatic
(local to function) storage.

--
Peter

Nov 14 '05 #3
<ge*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?


A const declaration creates a read-only variable. If you want a true
constant who's value is known at compile time, then do this:

#define N 10

--
Daniel Rudy
Nov 14 '05 #4
ge*****@gmail.com wrote:
How come this is illegal?

const int N = 10;
double a[ N ];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?


There is no syntax problem here; no "syntax rule" is violated.
This is a semantic violation, namely, the syntactically correct but
non-constant `N' is used in a context where a constant expression
is required. (As others have explained, `const' is only 62.5%
`constant'.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #5

Daniel Rudy wrote:
A const declaration creates a read-only variable. If you want a true constant who's value is known at compile time, then do this:

#define N 10


Wasn't the point of having "const" (in part) to be able to replace
#define?

Nov 14 '05 #6
ge*****@gmail.com wrote:
How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?


This is a flaw in the design of the C computer programming language
which was fixed in the C++ computer programming language
but could not be fixed in C
because so much legacy code depended upon it.
Nov 14 '05 #7
ge*****@gmail.com wrote:
Daniel Rudy wrote:
A const declaration creates a read-only variable. If you want a true


constant who's value is known at compile time, then do this:

#define N 10

Wasn't the point of having "const" (in part) to be able to replace
#define?

Yes. In *C++*. Not C.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #8
ge*****@gmail.com wrote:
Daniel Rudy wrote:
A const declaration creates a read-only variable. If you want a
true constant who's value is known at compile time, then do this:

#define N 10


Wasn't the point of having "const" (in part) to be able to replace
#define?


Not in the C language. If you want C++ that is another newsgroup.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
E. Robert Tisdale wrote:
ge*****@gmail.com wrote:
How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any
function". What's the reason behind this syntax rule?


This is a flaw in the design of the C computer programming
language which was fixed in the C++ computer programming
language but could not be fixed in C because so much legacy
code depended upon it.


C took the const keyword from C++. Thus, C++ did not 'fix'
existing C usage of const, and there was no 'legacy code'
to speak of.

--
Peter

Nov 14 '05 #10
At about the time of 5/10/2005 6:50 PM, ge*****@gmail.com stated the
following:
Daniel Rudy wrote:
A const declaration creates a read-only variable. If you want a true


constant who's value is known at compile time, then do this:

#define N 10

Wasn't the point of having "const" (in part) to be able to replace
#define?


AFAIK, that applies only in C++. In C, you still have to use #define
for true compile time constants.

--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.
Nov 14 '05 #11
On Tue, 10 May 2005 19:17:30 -0700, E. Robert Tisdale wrote:
ge*****@gmail.com wrote:
How come this is illegal?

const int N = 10;
double a[N];

I get "error: variable-size type declared outside of any function".
What's the reason behind this syntax rule?


This is a flaw in the design of the C computer programming language
which was fixed in the C++ computer programming language
but could not be fixed in C
because so much legacy code depended upon it.


How would "fixing" this in C break legacy code?

Lawrence
Nov 14 '05 #12

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

Similar topics

8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
1
by: SerGioGio | last post by:
Hello, I want to build a ".lib" library where I will have some constants. The constants are not only int, char, but also struct constants. How do I declare / implement these struct constants? I...
1
by: martin | last post by:
Hi, I am having a little trouble defining compiler constants and picking then up in my code. In the configuration manger I defined the constant "Martin" I then set the Project Configuration...
2
by: Marty | last post by:
Hi, In VB.NET, what happen to constants when compiling? Are they copied at every place they are needed? Or the application has to refer to constants address each time it need it at run time? ...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
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,...
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
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,...
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,...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
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.