B
Bilgehan.Balban
Hi,
The following code:
#include <stdio.h>
// const int const_asize = 10;
#define define_asize = 10;
int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};
int main(int argc, char * argv[])
{
return 0;
}
Using the #define compiles with no errors, however if you replace it
with the constant variable, I get the following error:
"error: variable-size type declared outside of any function"
"error: variable-sized object may not be initialized"
this is on gcc compiling with the following: gcc -o consttest main.c
adding a -std=c99 does not make any difference.
So the compiler differentiates between a literal and a const int when
they're declared global, but make no distinction when they're local. My
question is, is this standard C behaviour? i.e. Has it been defined as
part of C to refuse to compile above code with a const int specifying
array size, when the array is declared global?
Thanks,
Bahadir
The following code:
#include <stdio.h>
// const int const_asize = 10;
#define define_asize = 10;
int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};
int main(int argc, char * argv[])
{
return 0;
}
Using the #define compiles with no errors, however if you replace it
with the constant variable, I get the following error:
"error: variable-size type declared outside of any function"
"error: variable-sized object may not be initialized"
this is on gcc compiling with the following: gcc -o consttest main.c
adding a -std=c99 does not make any difference.
So the compiler differentiates between a literal and a const int when
they're declared global, but make no distinction when they're local. My
question is, is this standard C behaviour? i.e. Has it been defined as
part of C to refuse to compile above code with a const int specifying
array size, when the array is declared global?
Thanks,
Bahadir