A prefix operator immediately precedes its operand, as in −x.
Questions tagged [prefix-operator]
94 questions
393
votes
17 answers
Why avoid increment ("++") and decrement ("--") operators in JavaScript?
One of the tips for jslint tool is:
++ and --
The ++ (increment) and -- (decrement)
operators have been known to contribute to bad code by
encouraging excessive trickiness. They
are second only to faulty architecture
in enabling to viruses and…

artlung
- 33,305
- 16
- 69
- 121
381
votes
10 answers
What does "!--" do in JavaScript?
I have this piece of code (taken from this question):
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err)
return done(err);
var pending = list.length;
if…

Kieran E
- 3,616
- 2
- 18
- 41
59
votes
13 answers
What is the difference between prefix and postfix operators?
The following code prints a value of 9. Why? Here return(i++) will return a value of 11 and due to --i the value should be 10 itself, can anyone explain how this works?
#include
main()
{
int i= fun(10);
printf("%d\n",--i);
}
int…

pradeep
- 607
- 1
- 6
- 3
14
votes
3 answers
Why are Postfix ++/-- categorized as primary Operators in C#?
Currently I'm teaching a class of C++ programmers the basics of the C# language. As we discussed the topic operators I used C# standard categories of primary, unary etc. operators.
One of the attendees felt puzzled, because in the C# standard the…

Nico
- 1,554
- 1
- 23
- 35
12
votes
3 answers
Why does postfix operator++ have higher precedence than prefix operator++?
Defined this way, we can do neither ++x++ nor ++x--. But on the other hand, both (++x)++ and (++x)-- are useful expressions: (++x)++ increments x by two and returns the value "in the middle", while (++x)-- is essentially equivalent to x+1 but…

leftaroundabout
- 117,950
- 5
- 174
- 319
11
votes
4 answers
overloading postfix and prefix operators
please consider following code
#include
using namespace std;
class Digit
{
private:
int m_digit;
public:
Digit(int ndigit=0){
m_digit=ndigit;
}
Digit& operator++();//prefix
Digit&…
user466534
9
votes
2 answers
Why chained prefix increment/decrement for builtin type is not UB for C++?
In cpprefernce.com example for prefix increment there is such code:
int n1 = 1;
...
int n3 = ++ ++n1;
Why chained increment in this case does not lead to UB? Is rule for at most once modified not violated in this case?

Slava
- 43,454
- 1
- 47
- 90
9
votes
1 answer
unsequenced modification and access to pointer
I'm getting a warning for this C expression:
*p0++ = mult(*p0, psign[i1]);
The warning is:
unsequenced modification and access to 'p0' [-Wunsequenced]
I think the expression should be modified to this:
*p0 = mult(*p0, psign[i1]);
p0++;
Is the…

user1884325
- 2,530
- 1
- 30
- 49
8
votes
4 answers
How to differentiate (when overloading) between prefix and postfix forms of operator++? (C++)
Because I've overloaded the operator++ for an iterator class
template
typename list::iterator& list::iterator::operator++()
{
//stuff
}
But when I try to do
list::iterator IT;
IT++;
I get a warning about there being no…
user98188
8
votes
3 answers
Java prefix and unary operators together
I was working on Java prefix operators and came across this behavior
i = +--j //does not give an error
i = -++j //does not give an error
i = ---j //gives an error
i = +++j //gives an error
Why is this happening?

Kiran
- 388
- 2
- 16
8
votes
4 answers
Prefix form of unary operator in Haskell
In GHCi:
Prelude> (+3) 2
5
Prelude> (*3) 2
6
Prelude> (/3) 2
0.6666666666666666
Prelude> (-3) 2
No instance for (Num (t -> t1))
arising from the literal 3' at :1:2
Possible fix: add an instance declaration for (Num…

aXqd
- 733
- 5
- 17
7
votes
2 answers
Does Postfix operator really has a higher precedence than prefix?
However It is clearly written in precedence table that postfix operator has higher priority than prefix. But still I have a daubt.
I start with following example:
*ptr++; // evaluate as *(ptr++);
++*ptr; // evaluate as ++(*ptr);
This proves…

A.s. Bhullar
- 2,680
- 2
- 26
- 32
7
votes
1 answer
compilation order and post prefix opertors
I was wondering why the following outputs 7 7 6 7 instead of 5 6 6 7
my $a = 5;
printf("%d %d %d %d",$a,++$a , $a++ , $a);
I'm pretty sure it has something to do with the order of parameters compilation
Thanks,

snoofkin
- 8,725
- 14
- 49
- 86
6
votes
4 answers
Javascript increment while assigning
I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement.
When running this code:
var x = 0;
x = ++x;
is the second line equivalent to:
x = (x = x + 1) OR
x = (x + 1)
It is hard to tell the…

Asad Saeeduddin
- 46,193
- 6
- 90
- 139
5
votes
1 answer
Creating New Operator
I'm trying to make ¬ a logical negation operator.
¬ True;
multi sub prefix:<¬> ($n) {
return not $n;
}
When I run the above program, it returns this error:
$ perl6 test.pl6
===SORRY!=== Error while compiling /home/devXYZ/test.pl6 Bogus…

user6189164
- 667
- 3
- 7