Skip to content
Related Articles

Related Articles

Improve Article

A shorthand array notation in C for repeated values

  • Difficulty Level : Easy
  • Last Updated : 18 Sep, 2017

In C, when there are many repeated values, we can use a shorthand array notation to define array. Below program demonstrates same.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.




// C program to demonstrate working of shorthand
// array rotation.
#include <stdio.h>
  
int main()
{
    // This line is same as
    // int array[10] = {1, 1, 1, 1, 0, 0, 2, 2, 2, 2};
    int array[10] = {[0 ... 3]1, [6 ... 9]2};
  
    for (int i = 0; i < 10; i++)
        printf("%d ", array[i]);
    return 0;
}

Output:

1 1 1 1 0 0 2 2 2 2 

Note that middle gap of 2 is automatically filled with 0.

This article is contributed by Kaushik Annangi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :