Grouping Operator

Ignoring all of the other characters in a program, parenthesis always occur balanced. That means that a left parenthesis must occur first, and to the right of it there must be a matching right parenthesis, but there can be intervening nested parenthesis, as in the following examples:

	()()()
	(()())
	((()(())(()))())

Parenthesis have two completely different uses in a program. One is as a function parameter list delimiter. In this usage they occur after the identifier which is the name of the function, optionally with white space in-between. If there are any parameters to the function then they will appear between the parenthesis as a comma separated list of expressions (in actual use) or as formal parameters (type followed by formal parameter name) in a function prototype.

	printf("I am an expression %d", me+2);
	double sqrt(double I_am_a_formal_parameter);

The second use of parenthesis is to reorder expression evaluation. Without parenthesis in an expression, the normal operator precedence determines which operations are done in what order. With parenthesis we can change this order to be what we wish, but only for operators of different precedence. Thus the expression:

	x+y*z

would be evaluated such that first 'y' is multiplied by 'z', and that result is added to 'x', implicitly:

	x+(y*z)

but

	(x+y)*z

would add 'x' and 'y' together first and then multiply that result by 'z'.

When you are not sure about the operator precedence, put parenthesis. It will not change the size or speed of the resulting program. Subtle bugs can be avoided in this way.


© 1991-2008 Prem Sobel. All Rights Reserved.