| | |
postfix/prefix notations
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
In C and C++ the only time it makes a difference is with the increment and decrement operators, which each come in two forms: postfix and prefix.
Here we have an integer:
Let's play with it:
In this example, a was incremented before the expression was evaluated. (The entire cout statement.) This is prefix: the increment is done first.
In this example, a was incremented after the expression was evaluated. This is postfix: the increment is done last.
There is a caveat. For each distinct variable, you cannot use more than one increment or decrement operator in the same statement. Hence:
is likely to get you a random number.
Hope this helps.
Here we have an integer:
int a = 42;Let's play with it:
cout << a << endl; prints 42cout << ++a + 1 << endl; prints 44cout << a << endl; prints 43In this example, a was incremented before the expression was evaluated. (The entire cout statement.) This is prefix: the increment is done first.
cout << a << endl; prints 43cout << a++ + 1 << endl; prints 44cout << a << endl; prints 44In this example, a was incremented after the expression was evaluated. This is postfix: the increment is done last.
There is a caveat. For each distinct variable, you cannot use more than one increment or decrement operator in the same statement. Hence:
cout << ++a++ << ++a << endl; BAD!is likely to get you a random number.
Hope this helps.
Last edited by Duoas; Jan 29th, 2008 at 1:37 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: DirectX and C++ DrawIndexedPrimitive question
- Next Thread: Homework help; For loop
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






