Is this allowed in C Language as given below:
int a=2, b=3;
int arr[a+b];
Is this a valid C statement?
Eric Postpischil
232k15 gold badges197 silver badges377 bronze badges
2 Answers 2
An array declare with a size that is not a integer constant expression is called a variable length array. It is allowed in C, although not for arrays with static or thread storage duration.
The 1999 C standard required C implementations to support variable length arrays. The 2011 standard made support optional.
answered Nov 22, 2020 at 11:49
Eric Postpischil
232k15 gold badges197 silver badges377 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can do something like this:
#include <stdio.h>
#define A 2
#define B 3
int main(){
int a=A, b=B, arra[A+B];
return 0;
}
answered Nov 22, 2020 at 12:01
Gonçalo Bastos
4206 silver badges23 bronze badges
1 Comment
Gonçalo Bastos
@4386427 No but, thats the best option! I will edit it
lang-c
int a=2,b=3; int arr[a+b];?arris a pointer or array object declared.. somewhere. And even then, it's pointless as it doesn't actually do anything.#define A = 2 #define B = 3 int arr[A+B];