Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Parentheses and Priority

Node:Parentheses and Priority, Next:, Previous:Expressions, Up:Expressions and operators



Parentheses and Priority

Just as in algebra, the C compiler considers operators to have certain priorities, and evaluates, or parses, some operators before others. The order in which operators are evaluated is called operator precedence or the order of operations. You can think of some operators as "stronger" than others. The "stronger" ones will always be evaluated first; otherwise, expressions are evaluated from left to right.

For example, since the multiplication operator * has a higher priority than the addition operator + and is therefore evaluated first, the following expression will always evaluate to 10 rather than 18:

4 + 2 * 3

However, as in algebra, you can use parentheses to force the program to evaluate the expression to 18:

(4 + 2) * 3

The parentheses force the expression (4 + 2) to be evaluated first. Placing parentheses around 2 * 3, however, would have no effect.

Parentheses are classed as operators by the compiler; they have a value, in the sense that they assume the value of whatever is inside them. For example, the value of (5 + 5) is 10.

 
 
  Published under free license. Design by Interspire