A Magellan Four Star Site Iftech.comHomeAbout ITIGuestbookSearch ITI

Online Training CenterHelp

Introduction to C Programming

Part 14: Operator Precedence in C

by Marshall Brain (questions@iftech.com)
The second edition of "Developing Professional Applications in Windows 95 and Windows NT Using MFC" is now available. This book is filled with tons of great examples and code to get you up and running with VC++ 4.2 and beyond! See the book description and TOC for more information. Order online from amazon.com


Operator Precedence in C

C contains many operators, and because of the way in which operator precedence works, the interactions between multiple operators can become confusing.


 

x=5+3*6; 

X receives the value 23, not 48, because in C multiplication and division have higher precedence than addition and subtraction.


 

char *a[10]; 

Is a a single pointer to an array of 10 characters, or is it an array of 10 pointers to character? Unless you know the precedence conventions in C, there is no way to find out. Similarly, in E.11 we saw that because of precedence statements such as *p.i = 10; do not work. Instead, the form (*p).i = 10; must be used to force correct precedence.

The following table from Kernigan and Richie shows the precedence hierarchy in C. The top line has the highest precedence.


Operators                                   Associativity

( [ - .                                     Left to right

! - ++  -{-  + *  &  (type-cast)  sizeof    Right to left

(in the above line, +, - and * are 

the unary forms) 

*  / %                                      Left to right

+  -                                        Left to right

<<  >>                                      Left to right

<  <=  >  >=                                Left to right

==  !=                                      Left to right

&                                           Left to right

^                                           Left to right

|                                           Left to right

&&                                          Left to right

||                                          Left to right

?:                                          Left to right

=  +=  -=  *=  /=  %=  &=  ^=  |=  <<= >>=  Right to left

,                                           Left to right 

Using this table, you can see that char *a[10]; is an array of 10 pointers to character. You can also see why the parentheses are required if (*p).i is to be handled correctly. After some practice, you will memorize most of this table, but every now and again something will not work because you have been caught by a subtle precedence problem.

<- BackForward ->Return


Iftech.comHomeAbout ITIGuestbookSearch ITI

Interface Technologies, Inc.
(800)224-4965 - Highwoods Office Center - 3120 Poplarwood Ct, Suite 104 - Raleigh, NC 27604

Questions or comments about Interface Technologies products and services, email:
interface@iftech.com

Questions or comments about Iftech.com, email:
webmaster@iftech.com

© 1997 Interface Technologies, Inc

Microsoft Internet Explorer Netscape Navigator 3.0