A shorthand array notation in C for repeated values?


An array stores number of same data type. For an array there may arise a situation when you need to store 2-3 values that are same i.e. 3,3,3,3 needs to be stored.

For this case, the programing language C has made a simple way to create an array with such repeated value to reduce the workload of the programmer.

Syntax

[startofRepeatingSeq … EndofRepeatingSeq]number
Example : For 3 repeated 5 times ;
[0 … 4]3

Example

#include <stdio.h>
int main() {
   int array[10] = {[0 ... 4]3, [6 ... 9]5};
   for (int i = 0; i < 10; i++)
      printf("%d ", array[i]);
   return 0;
}

Output

3 3 3 3 3 0 5 5 5 5

Updated on: 07-Aug-2019

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements