-1

I am learning how to program arrays in C. I have a question about the following code regarding size of array. In the below code, is it a valid declaration of size of an array? Please explain me if it is either valid or invalid.

#include<stdio.h>
#define SIZE 10

int main(void) {
  int size=12;
  float salary[size];
  salary[0]=890.54;
  printf("%f",salary[0]);
  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

It's valid size value (only with C99). You can refer from this URL for more detail: https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/Arrays.html

Lam Dinh
  • 21
  • 4
2

To support the other answer, if variable length arrays (VLA) are supported then yes — the declaration in the question is valid and defines a VLA. VLAs made their debut in C99 and then in C11 they are made optional. A conforming C11 compiler that does not support VLAs defines __STDC_NO_VLA__.

From §6.7.6.2¶4

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations or type names with function prototype scope;143) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type. (Variable length arrays are a conditional feature that implementations need not support; see 6.10.8.3.)

from §6.10.8.3¶1

__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2736738
  • 30,591
  • 5
  • 42
  • 56