2

I have a

int val,i;
scanf("%d",&val);
int a[] = { (val & 0x7) , (val & 0x5) };
for(i=0;i<2;i++)
printf("%d\n",a[i]);

I am confused whether the above code is good or not? I get this error on some compiler

expression must have a constant value

but on another this works fine.So is this code good?

4
  • On which compiler it does not work? Commented Mar 12, 2015 at 10:57
  • How many elements does a have? If you need two, why not to define int a[2]? Commented Mar 12, 2015 at 11:07
  • @a3mlord This is just an example I need something like this Commented Mar 12, 2015 at 11:08
  • @a3mlord; You need an update. Commented Mar 12, 2015 at 11:09

1 Answer 1

4

Variables in initializer list is not allowed before C99.

C89: 6.5.7:

All the expression in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

From C99, it is allowed. Make sure you are compiling with -std=c99 flag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.