Search⌘ K

Defining the Size of an Array

Understand the rules for defining array size in C programming. Explore why array size must be a positive integer constant, learn why runtime variables cannot specify size, and discover how to use preprocessor directives like #define to set array sizes effectively.

Defining the size by specifying the size before the array name

We cannot define an array by first specifying the array size and then the array name in square brackets.

Error: The code given below will generate an error.

#include<stdio.h>
int main() {
    int a[5];
    /* Uncommenting line 6 will generate an error
    */ 
    //int 5[b];
    // First write array name and then specify subscript in brackets
    printf("%d ", a[2]);
    // First specify array subscript and then write array name in square brackets
    printf("%d ", 2[a]);
}

We cannot use the form, ...