This question already has answers here:
variably modified array at file scope in C
(4 answers)
"static const" vs "#define" vs "enum"
(17 answers)
Defining the size of an array using a const int
(3 answers)
Closed 3 years ago.
int const a=9;
int stack[a];
int main()
{
return 0;
}
The above code gives an error:variably modified 'stack' at file scope
But when I change the code to :
#define b 3
int stack[b];
int main()
{
return 0;
}
It compiles without error.While both #define and const variable are used for defining the constant identifier then why there is error when I use the const var instead of #define.I searched the similar question but all they gave solution about the error but no reason to it.
Searched about the const and #define and found that sometimes gcc compiler recognizes const as readonly but it is too confusing.
In C language static storage variables can have size defined by the constant expression. Using the variable (even the constant one) as the size is not such a expression.
The error is 100 percent correct.
The second case: proprocessor replaces textually the b with 3 which is the constant expression
Constant expression is something which is evaluated during the compilation. Variable value can be only evaluated runtime.
Related
This question already has answers here:
declaring a variable-length array as a global variable in C
(4 answers)
Closed 2 years ago.
I'm trying to understand the difference that the scoping makes:
//global scope
int size = 4;
int array[size]; // error: variably modified 'array' at file scope
int main(void) {
int buff[size]; // works!
}
how does using a variable as array size doesn't work globally but works inside main? It would work if I use a macro instead.
Also, does using const matter for size?
simply make it compile time integral constant expression, since array length must be specified at the compile time, with define
#define SIZE 6
int array[SIZE];
This question already has answers here:
Why can I change a local const variable through pointer casts but not a global one in C?
(7 answers)
Can we change the value of an object defined with const through pointers?
(11 answers)
In C, can a const variable be modified via a pointer?
(4 answers)
Closed 3 years ago.
I read that constant values cant be changed but in this code below the value of i is getting changed through the use of a pointer. May I know how?
#include <stdio.h>
int main()
{
const int i = 10;
int *ptr = &i;
*ptr = 20;
printf("%d\n", i);
return 0;
}
The output of this code is 20 with a compiler warning.
The behaviour of your code is undefined - the language does not define the behaviour of modifying an object originally declared as const via a pointer that's had const stripped from it.
On some compilers with optimisations turned on, getting 10 as the output is reasonable.
This question already has answers here:
In C, why can't a const variable be used as an array size initializer? [duplicate]
(3 answers)
Closed 4 years ago.
Why is it not possible to declare an array outside of a function with variables as size parameters in C99?
For example, consider this code snippet.
It results in an error: variably modified ‘matrix’ at file scope compile error.
static int const height = 5;
static int const width = 5;
static int const matrix[height][width] = { ... };
int main(void){ ... }
I know that const in c doesn't mean constant. It means "read only", but I don't properly understand what implications this have.
So why can't arrays get their size from read-only memory?
I know this problem can be solved using #defines or enum so i am more interested in an explanation as to why this is the case.
C99 6.7.5.2/2 Array declarators:
Only ordinary identifiers (as defined in 6.2.3) with both block scope or function prototype scope and no linkage shall have a variably modified type. If an identifier is declared to be an object with static storage duration, it shall not have a variable length array type.
This question already has answers here:
In C, why can't a const variable be used as an array size initializer? [duplicate]
(3 answers)
Closed 5 years ago.
I'm trying to create a struct with an array inside. The array size, I'm hoping, should be set at compile time. That is it's hard coded but uses a variable so I can change it easily in the code. Problem is I'm getting linker errors when I use const int in the header ahead of the struct definition. Here's my code:
from the header file:
const int t_Module_qInternalParams =64;
typedef struct Module{
double internalParams[t_Module_qInternalParams];
} t_Module;
This:
const int t_Module_qInternalParams = 64;
Is a constant in the sense that the object cannot be modified after initialization, but it's still a variable. Especially, t_Module_qInternalParams is not a compile-time constant, as required in your declarator.
A simple solution is to use a preprocessor macro instead:
#define MODULE_INTERNALPARAMS 64
This just expands to 64 before the compiling phase starts, and of course, 64 is a compile-time constant.
This question already has answers here:
"static const" vs "#define" vs "enum"
(17 answers)
Why would someone use #define to define constants?
(9 answers)
Closed 8 years ago.
I would like to understand the difference between defining a constant variable as follows:
const int ONE = 1;
and using a preprocessor directive:
#define ONE (1)
I know that in the second case "1" gets in some sense hardcoded and the compiler does not even see the variable ONE, but I am not sure about the first case. The fact of declaring the variable as constant just prevents from accidentally change its value, or does the compiler catch the opportunity to do some optimization? Is there any significant benefit of one approach over the other?
In C, const int c; means that c can't be modified during the run of program. However, c is not a constant during compile time and can not be used in constant expressions. So for example the program:
const int MAX = 10;
int a[MAX];
does not compile, while:
#define MAX 10
int a[MAX];
does.
In C++, const variables are true compile-time constants, so there may be less reasons to use #define for it. An example where #define is necessary is when you need to use the constant in an #if directive.