Aman Sharma
Aman Sharma

Reputation: 1

Array declaration problem in C with variables

Is this allowed in C Language as given below:

int a=2, b=3;
int arr[a+b];

Is this a valid C statement?

Upvotes: 0

Views: 86

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 224596

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.

Upvotes: 3

Gonçalo Bastos
Gonçalo Bastos

Reputation: 421

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;
}

Upvotes: 1

Related Questions