What will be difference b/w 5th and 6th statements?

1. #define MAX 100
2. main()
3. {
4. int max=100;
5. int a[MAX];
6. int b[max];
7. }

Questions by Pramoda.M.A

Showing Answers 1 - 21 of 21 Answers

vinayps03

  • Aug 6th, 2008
 

Size of an array can't be dynamically resized at runtime (one of the reasons y v use data structures!)
5th line: size of a[MAX] is constant (100) --> so no probs here.
6th line: Compilation error..
c:Code(6) : error C2057: expected constant expression

c:Code(6) : error C2466: cannot allocate an array of constant size 0

c:Code(6) : error C2133: 'b' : unknown size

kalai4best

  • Aug 7th, 2008
 

max can be used only within the main() and has a impact within that. But MAX can be used in any kind of user defined functions.

  Was this answer useful?  Yes

mayankjaan

  • Sep 19th, 2008
 

On 5th statement work fine.
But on 6 the statement it gives compilation error.


error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'b' : unknown size


Conclusion:
The macro value expansion done at compilation time but the assignment of
variable value at run time.
Also the array has property to assign constant value not and variable value

  Was this answer useful?  Yes

5th statement, here the array size will be assigned at preprocessor stage.

6th statement, here it will not give any compile time error but some the program will be having lot of bugs. size will be assigned at run time.

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

In C99, the differences are that b is defined, rather than a, and that b is a variable-length-array rather than a conventional array.

In C90, the sixth line is an error, as C90 does not support VLAs.

  Was this answer useful?  Yes

Line 6 defines a variable-length array, which is only valid for C99 or later.  The size of a VLA is not known until runtime, they may not declared static or extern, and only ordinary identifiers (not structure or union members) may be declared as VLAs. 

In C89 it's a syntax error. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions