Search This Blog

Tuesday, April 22, 2014

Ellipses Operator (. . .)

This operator is used to represent various number of parameters involve. This is mainly used in function arguments , when the number of arguments is not known.

Ex -
int foo(int i, int j, . . .);

But we can use ellipses operator in other part of coding also.

In switch case

main()
{
       --------
       --------
       switch(3)
       {
               case 1 ... 4:
                    printf("Option is between 1 to 4);
                    break;

               case 5 ... 9:
                    printf("Option is between 5 to 9);
                    break;
        }
}

In array

Ellipses operator used in array initialization also. Using this operator we can partially initialize our arrays.

Ex - 

main()
{
         int arr[] = { [0 ... 4] 4, [5 ... 9] 5};

         printf("arr[0] %d\n", arr[0]);
         printf("arr[6] %d\n", arr[6]);
}

o/p - 
arr[0] 4
arr[6] 5


Partially initialize the array 

main()
{
        int arr[10] = { [3]5, [1] = 4, [4] 3, [0] = 0 };

         printf("arr[0] %d\n", arr[0]);
         printf("arr[3] %d\n", arr[3]);
         printf("arr[4] %d\n", arr[4]);
}
o/p - 
arr[0] 0
arr[3] 5
arr[4] 3


1 comment:

  1. Hi Mukesh kumar,

    You shared very good information. In addition to your info i am adding some more.

    1 --> The above features are not available in ANSI Standard C, available only in GCC.
    2 --> GCC will throw warnings If you compile with -pedantic option.
    3 --> These features are comes under C Extensions.

    Thanks Mukesh, Don't stop, Keep going

    ReplyDelete