473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

const int x=5 ;

hi,
Can anyboby please tell me that why the following code is'nt compiling:

int main(void)
{
const int a=5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i =5;
Thanks

Nov 14 '05 #1
15 3315
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:

int main(void)
{
const int a=5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i =5;


Firstly, in C language 'const int i =5' is not a statement, it is a
declaration.

Secondly, there's no 'const int i =5' in your code. You probably meant
'const int a=5'.

Thirdly, in C language constant integral objects do not qualify as
'integer constants' and cannot be used in integral constant expressions
(ICE).

In the original C language (C89/90) array size in array declaration must
be an ICE. In your code it is not. That's why the code is not compiling
(assuming that you are using a C89/90 compiler).

In C99 the array size in such declaration is not required to be an ICE,
meaning that in C99 this code would be legal and 'buf' would become a
variable-length array (VLA).

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #2
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:

int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int i = 5; cat main.c #include <stdio.h>

int main(void) {
const
int n = 5;
int buf[n];
for (int j = 0; j < n; ++j)
buf[j] = j;
for (int j = 0; j < n; ++j)
fprintf(stdout, "buf[%d] = %d\n", j, buf[j]);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

buf[0] = 0
buf[1] = 1
buf[2] = 2
buf[3] = 3
buf[4] = 4

It seems to work just fine for me.
Nov 14 '05 #3
Andrey Tarasevich wrote:
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:
int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int a = 5;


In C language [standards documents],
'const int a = 5' is not a statement, it is a declaration.


It *is* a statement. A declaration *is* a statement.
Declarations are distinguished from statements
only in the context of the ANSI/ISO C standards documents.
Nov 14 '05 #4
E. Robert Tisdale wrote:
...
Can anyboby please tell me that why the following code is'nt compiling:
int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int a = 5;


In C language [standards documents],
'const int a = 5' is not a statement, it is a declaration.


It *is* a statement. A declaration *is* a statement.
Declarations are distinguished from statements
only in the context of the ANSI/ISO C standards documents.


Sorry, but strictly defined notions of 'declaration' and 'statement'
also exist only within the contexts of C standard documents. And in
those contexts, declaration is not a statement.

If you are thinking about some kind of your own invented context, in
which 'declarations' are 'statements', the you should introduce that
context first, so that everyone here knows what you are talking about.
Without that, what you are saying doesn't make any sense.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #5
Andrey Tarasevich wrote:
Sorry, but strictly defined notions of 'declaration' and 'statement'
also exist only within the contexts of C standard documents.
And in those contexts, declaration is not a statement.


Sorry, but the ANSI/ISO C standards documents are off topic
in the comp.lang.c newsgroup. Try the comp.std.c newsgroup.
Nov 14 '05 #6
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:
int main(void) {
const int a = 5;
int buf[a];
return 0;
}
There seems to be some error in the statement const int i = 5;

> cat main.c

#include <stdio.h>

int main(void) {
const
int n = 5;
int buf[n];
for (int j = 0; j < n; ++j)
buf[j] = j;
for (int j = 0; j < n; ++j)
fprintf(stdout, "buf[%d] = %d\n", j, buf[j]);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

buf[0] = 0
buf[1] = 1
buf[2] = 2
buf[3] = 3
buf[4] = 4

It seems to work just fine for me.


Of course it does. You're using a C99 compiler (actually an
incomplete one) that supports VLAs. The OP is presumably using a
compiler that doesn't support VLAs, probably a C90 compiler. If you
had mentioned that, you could actually have answered the OP's
question.

Using your own version of the program:

% gcc -Wall -std=c99 -pedantic -o main main.c
% gcc -Wall -std=c89 -pedantic -o main main.c
main.c: In function `main':
main.c:6: warning: ISO C90 forbids variable-size array `buf'
main.c:7: error: `for' loop initial declaration used outside C99 mode
main.c:9: error: redeclaration of `j'
main.c:7: error: `j' previously declared here
main.c:9: error: `for' loop initial declaration used outside C99 mode

Support for C89/C90 is nearly universal. Support for C99 is not.
Pretending that it is doesn't help anyone.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #7
Keith Thompson wrote:
E. Robert Tisdale writes:
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:

int main(void) {
const int a = 5;
int buf[a];
return 0;
}
There seems to be some error in the statement const int a = 5;
> cat main.c

#include <stdio.h>

int main(void) {
const
int n = 5;
int buf[n];
for (int j = 0; j < n; ++j)
buf[j] = j;
for (int j = 0; j < n; ++j)
fprintf(stdout, "buf[%d] = %d\n", j, buf[j]);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

buf[0] = 0
buf[1] = 1
buf[2] = 2
buf[3] = 3
buf[4] = 4

It seems to work just fine for me.

Of course it does. You're using a C99 compiler
(actually an incomplete one) that supports VLAs.
The OP is presumably using a compiler that doesn't support VLAs,
probably a C90 compiler.


I don't know that and neither do you.
If you had mentioned that,
What do you think the '-std=c99' option means?
you could actually have answered the OP's question.
I don't know what Candy's problem is.
It didn't show us any diagnostic messages
or even tell us which compiler it was using.
Using your own version of the program:

% gcc -Wall -std=c99 -pedantic -o main main.c
Evidently, you *do* know what the '-std=c99' option means.
% gcc -Wall -std=c89 -pedantic -o main main.c
main.c: In function `main':
main.c:6: warning: ISO C90 forbids variable-size array `buf'
main.c:7: error: `for' loop initial declaration used outside C99 mode
main.c:9: error: redeclaration of `j'
main.c:7: error: `j' previously declared here
main.c:9: error: `for' loop initial declaration used outside C99 mode

Support for C89/C90 is nearly universal.
Support for C99 is not.
So what?
Must C programmers be forever constrained by obsolete compilers?
There are plenty of C99 compiler that will accept Candy's program.
Your protest is irrelevant.
Pretending that it is doesn't help anyone.

Nov 14 '05 #8
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Andrey Tarasevich wrote:
ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:

int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int a = 5;

In C language [standards documents], 'const int a = 5' is not a
statement, it is a declaration.


It *is* a statement. A declaration *is* a statement.
Declarations are distinguished from statements
only in the context of the ANSI/ISO C standards documents.


Your claim that a declaration is a statement is inconsistent both with
the ANSI/ISO C standards documents and with common usage.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #9
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
[...]
Sorry, but the ANSI/ISO C standards documents are off topic
in the comp.lang.c newsgroup.


Liar.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #10
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Keith Thompson wrote:
E. Robert Tisdale writes: [...]
It seems to work just fine for me. Of course it does. You're using a C99 compiler
(actually an incomplete one) that supports VLAs.
The OP is presumably using a compiler that doesn't support VLAs,
probably a C90 compiler.


I don't know that and neither do you.


That's why I wrote "presumably" rather than "certainly".

[...]
I don't know what Candy's problem is.
It didn't show us any diagnostic messages
or even tell us which compiler it was using.
Yes, more information would have been useful, but the assumption that
the OP is using a compiler that doesn't support C99 VLAs is the only
reasonable explanation, and one that should have immediately occurred
to any knowledgeable C programmer.

[...]
So what?
Must C programmers be forever constrained by obsolete compilers?
I hope not, but I'm willing to accept the reality that those
constraints have not yet been entirely eliminated.
There are plenty of C99 compiler that will accept Candy's program.


And there are plenty of C90 compilers that won't. Since Candy asked
why the code wasn't compiling, and since there's a single likely
explanation, saying that it works for you is unhelpful.

Even assuming a C99 implementation, you might at least have pointed
out that, given

const int n = 5;
int buf[n];

buf is a VLA, not an ordinary array as one might expect, because n is
not a constant expression.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #11
Keith Thompson wrote:
E. Robert Tisdale writes:
Andrey Tarasevich wrote:

ca********@yahoo.com wrote:
Can anyboby please tell me that why the following code is'nt compiling:

int main(void) {
const int a = 5;
int buf[a];
return 0;
}

There seems to be some error in the statement const int a = 5;

In C language [standards documents], 'const int a = 5' is not a
statement, it is a declaration.


It *is* a statement. A declaration *is* a statement.
Declarations are distinguished from statements
only in the context of the ANSI/ISO C standards documents.

Your claim that a declaration is a statement is inconsistent both with
the ANSI/ISO C standards documents and with common usage.


Java:

http://www.site.uottawa.ca:4321/java...ationstatement

Visual Basic:

http://msdn.microsoft.com/library/de...gVariables.asp

Fortran:

http://docs.hp.com/en/B3908-90002/ch10s68.html

C++:

http://www.cs.rochester.edu/u/srini/...CppPrimer.html

C:

http://www.netwind.com/html/c_programming.html

English:

http://www.bartleby.com/61/57/D0075700.html

SYLLABICATION: dec·la·ra·tion
NOUN: 1. An explicit, formal announcement, either oral or written.
2. The act or process of declaring.
3. A statement of taxable goods or of properties subject to duty.
4. Law a. A formal statement by a plaintiff specifying
the facts and circumstances constituting his or her cause of action.
b. An unsworn statement of facts that is admissible as evidence.
5. Games A bid,
especially the final bid of a hand in certain card games.
Nov 14 '05 #12
> Andrey Tarasevich wrote:
Sorry, but strictly defined notions of 'declaration' and
'statement' also exist only within the contexts of C
standard documents.
"Strictly defined notions" is an oxymoron! ;)

The terms declaration and statement are precise grammatical
elements of the C language.
And in those contexts, declaration is not a statement.

True.

E. Robert Tisdale wrote: Sorry, but the ANSI/ISO C standards documents are off topic
in the comp.lang.c newsgroup.


I'm not sorry in pointing out that the standards are precisely
relevent to comp.lang.c. How can you discuss C programming
without a reference to what the C language _is_?

Comp.std.c is for discussing whether the (most recent) C standard
reflects the intent of the C committee in defining a C language.

--
Peter

Nov 14 '05 #13
Keith Thompson wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
[...]
Sorry, but the ANSI/ISO C standards documents are off topic
in the comp.lang.c newsgroup.


Liar.


+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT | :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #14
Peter Nilsson wrote:
E. Robert Tisdale wrote:
.... snip ...
Sorry, but the ANSI/ISO C standards documents are off topic
in the comp.lang.c newsgroup.


I'm not sorry in pointing out that the standards are precisely
relevent to comp.lang.c. How can you discuss C programming
without a reference to what the C language _is_?


+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT | :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #15
On Fri, 25 Mar 2005 09:03:40 -0500, in comp.lang.c , do*@dot.dot
wrote:

(snip CBF's amusing troll ascii art)
Gees man, what are you? Grand Poobah of Control Freaks R Us?


If you've been around here for more than ten minutes, youll be familar
with our resident troll ERT, and will understand why we discourage
people from feeding him.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #16

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

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
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,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.