1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Rep Power

    Uses of Parentheses

    I see parentheses used in cast operators (ex. (double) x;)
    Functions - main() { }

    What are they exactly used for in the C programming language?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Please refer to this link.

    Cast: The syntax demands parenthesizing the type.

    As function arguments: Again, the syntax expression(possible argument list) denotes a function call or declaration or definition.

    Expressions: override operator precedence.

    preprocessor: denote macro arguments much like function notation.

    preprocessor: (usually) enclose arguments the definition in parentheses to avoid surprises.
    Example disaster:
      Possible macros to evaluate a cubic equation.
      The GOOD macro has parentheses around every occurrence of each argument.
      The GOOD macro is entirely enclosed with parenthesis.
    #define POOR(C,X)  C[0] + X * (C[1] + X * (C[2] + X * (C[3])))
    #define GOOD(C,X) ((C)[0] + (X) * ((C)[1] + (X) * ((C)[2] + (X) * ((C)[3]))))
    int coef[] = {-2,3,0,0,0,0,0,0}; /* linear,  y = 3x-2 */
    int main() {
      int printf(char*,...), puts(char*);
      puts("using good  using poor        expression");
      printf("   %4d        %4d           f(7), demonstrates both macros can work\n",
    	 GOOD(coef, 7), POOR(coef, 7));
      printf("   %4d        %4d           f(1-1) expect equivalence with f(0)\n",
    	 GOOD(coef, 1-1), POOR(coef, 1-1));
      printf("   %4d        %4d           f(2)*f(-2) expect -32\n",
    	 GOOD(coef, 2)*GOOD(coef, -2), POOR(coef, 2)*POOR(coef, -2)); 
      return 0;

    output for lazy and overworked people:
    using good  using poor        expression
         19          19           f(7), demonstrates both macros can work
         -2          -4           f(1-1) expect equivalence with f(0)
        -32         -20           f(2)*f(-2) expect -32
    Last edited by b49P23TIvg; July 20th, 2013 at 01:20 PM. Reason: insert output
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo