const int x=5 ;

C

candy_init

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
 
E

E. Robert Tisdale

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.
 
A

Andrey Tarasevich

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).
 
E

E. Robert Tisdale

Andrey said:
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.
 
A

Andrey Tarasevich

E. Robert Tisdale said:
...
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.
 
E

E. Robert Tisdale

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

Keith Thompson

E. Robert Tisdale said:
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.
 
E

E. Robert Tisdale

Keith said:
E. Robert Tisdale said:
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.
 
K

Keith Thompson

E. Robert Tisdale said:
Andrey said:
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.
 
K

Keith Thompson

E. Robert Tisdale said:
Keith said:
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.
 
E

E. Robert Tisdale

Keith said:
E. Robert Tisdale said:
Andrey Tarasevich wrote:

(e-mail address removed) 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/index.html#declarationstatement

Visual Basic:

http://msdn.microsoft.com/library/d.../en-us/vbcn7/html/vaconDeclaringVariables.asp

Fortran:

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

C++:

http://www.cs.rochester.edu/u/srini/csc172/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.
 
P

Peter Nilsson

"Strictly defined notions" is an oxymoron! ;)

The terms declaration and statement are precise grammatical
elements of the C language.

True.

E. Robert Tisdale said:
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.
 
C

CBFalconer

Keith said:

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

CBFalconer

Peter said:
E. Robert Tisdale wrote:
.... snip ...


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______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================
 
D

dot

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


Gees man, what are you? Grand Poobah of Control Freaks R Us?
 
M

Mark McIntyre

(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.
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top