The comma   (,) operator

  • The comma operator "glues" ("strings together") multiple expressions into one single expression

  • Syntax of the comma operator:

      expr1 , expr2 , ... , exprN 
    
      Effect:
              First evaluate expr1
    	  Then  evaluate expr2
    	  ...
    	  Finally evaluate exprN
    
    	  Return the (last) value of exprN

  • The usage of the comma operator:

      • All the expressions string together by the comma operator will count as 1 expression

Common usage of the comma operator

  • The most common usage of the comma operator is in the for-statement where you need to initialize multiple index variables

  • Example:

      • Write a for-loop that copies A[0], A[1], ..., A[9] to B[10], B[11], ..., B[19], respectively

    Answer:

       for ( i = 0 , j = 10;  i < 10;  i++, j++ )      
          B[j] = A[i];