C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2008, 06:54 AM   #1
Registered User
 
Join Date: May 2008
Posts: 2
Unhappy Little question about postfix and prefix

I thought this code:
Code:
{           int	myInt;
             myInt = 5;
             printf( "myInt++ ---> %d\n\n++myInt ---> %d", myInt++, ++myInt);
}
will yield:

"myInt++ ---> 5

++myInt ---> 7"

but it actually gave:

"myInt++ ---> 6

++myInt ---> 7"

with a warning: operation on 'myInt' may be undefined.

Can some one help me out? I'm so confused by the result and the warning message.
firebird is offline   Reply With Quote
Old 05-08-2008, 07:00 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,356
Read this C FAQ on expressions.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-08-2008, 07:21 AM   #3
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
Changing a variable more than once in a single statement is undefined.
cpjust is offline   Reply With Quote
Old 05-08-2008, 07:27 AM   #4
Registered User
 
Join Date: May 2008
Posts: 2
Thank you guys very much.
firebird is offline   Reply With Quote
Reply

Tags
postfix, prefix, printf

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is prefix faster than postfix? dwks C Programming 6 07-28-2005 11:51 AM
stacks postfix aww fooey drdodirty2002 C++ Programming 3 09-25-2004 10:00 AM
postfix and prefix drdodirty2002 C++ Programming 8 09-24-2004 06:26 AM
postfix and prefix...??? matheo917 C++ Programming 8 04-20-2002 01:19 PM
Data Structures / prefix and postfix rickc77 A Brief History of Cprogramming.com 1 12-08-2001 12:48 PM


All times are GMT -6. The time now is 03:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22