LINUX TIPS AND TRICKS --- October 13, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Postfix and Prefix Operators
By Danny Kalev

This week I will focus on a basic -- and yet tricky -- language
construct: postfix and prefix operators. Particularly for novices, this
is a fertile source of confusion and misconceptions.

--Differentiating between ++n and n++

In C, C++, Java and several other languages, the built-in ++ and --
operators can appear on both sides of their operand. For example:

++n; // prefix ++
n++; // postfix ++

You probably know that a prefix operator first changes its operand
before taking its value. For example:

int n=0, m=0;
n = ++m; // first increment m, then assign its value to n
printf("%d %d",n, m); /* display 1 1*/

In this example, n equals 1 after the assignment because the increment
operation took place before m's value was taken and assigned to n. By
contrast, 

int n=0, m=0;
n = m++; // first assign m's value to n, then increment m
printf("%d %d",n, m); /* display 0 1*/

In this example, n equals 0 after the assignment because the increment
operation took place after m's original value was taken and assigned to
n.
 
*********************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved.

http://www.itworld.com
