Questions tagged [pre-increment]

For issues relating to defining or performing pre increment operations.

Pre-increment operators increase the value of their operand by 1 (or another specified amount), and the value of the operand in the expression is the operand's value after the increment operation.

342 questions
4
votes
5 answers

++nc vs nc = nc + 1

In K&R Ch 1: The statement ++nc presents a new operator, ++, which means increment by one. You could instead write nc = nc + 1, but ++nc is more concise and often more efficient. When would pre-increment be more efficient than the alternative? For…
mwlow
  • 141
  • 10
4
votes
4 answers

Atypical uses for Javascript's ++ and -- operators

If I recall correctly from Crockford's "Javascript: The Good Parts", he is not in favor of using the ++ or -- operators, but I also tend to recall he doesn't provide an especially strong argument against them. Below is a use of these operators that…
Dexygen
  • 12,287
  • 13
  • 80
  • 147
4
votes
2 answers

When are the ++c and c++ increments applied exactly here?

Just to see how much I understood how the ++c/c++ operator works, I tried to run these C programs: int c = 5; c = c - c++; printf("%d\n", c); prints 1, I guess the logic is that the ++ is applied after the line of code where it's used, so c becomes…
memememe
  • 663
  • 6
  • 21
4
votes
3 answers

pass by reference with increment operator

Hello all I have a very basic question while learning to code in c++. I am reading about the difference between pass by reference and pass by value. I wrote a simplistic code to test it but it does something I didn't expect. #include…
vkaushal
  • 41
  • 3
4
votes
1 answer

PHP Post and preincrement

I have found in PHP some strange calculation, for example this: $c=5; $r = $c + ($c++ + ++$c); echo $r; Why result is 19 and not 17? Thanks
NeroBuggy
  • 67
  • 2
4
votes
1 answer

Why is the output of `j= ++i + ++i;` different in C# and C?

int i=1,j; j= ++i + ++i; printf("%d",j); The output of this program is 6 in C.But when I use the same logic for C#, the output is 5 . I want to know the reason why the same logic behaves differently in two different languages
Dhruvin shah
  • 197
  • 1
  • 12
4
votes
3 answers

Difference in Increment-decrement operator in C and JAVA

Please consider the following statement: int a[]={1,2,3,4,5,6,7,8}; int i=0,n; n=a[++i] + i++ + a[i++] + a[i] ; According to my logic n should be 10. But I am getting different output in c (output is 7) However in java I am getting expected result…
Abhas Tandon
  • 1,859
  • 16
  • 26
4
votes
1 answer

Why is there no difference in ++foo and foo++ when the ++ operator is overloaded?

Possible Duplicate: Post-increment Operator Overloading Why are Postfix ++/— categorized as primary Operators in C#? I saw that I can overload the ++ and -- operators. Usually you use these operators by 2 ways. Pre and post increment/deccrement…
Bosak
  • 2,103
  • 4
  • 24
  • 43
4
votes
4 answers

Behaviour of preincrement operator in C program

I am running the following C code: #define cube(x) (x*x*x) void main() { int x=2,y; y=cube(++x); printf("%d %d",++x,y); } I am expecting result as 6,60 But it is giving different result. I think I have…
Jainendra
  • 24,713
  • 30
  • 122
  • 169
3
votes
0 answers

The below two C codes gives two different outputs, can someone explain why?

The first code is : #include int main() { int a,b,c; a = 4; b = ++a; c = b + a++; printf("%d",c); return 0; } Output is 10 The second code is : #include int main() { int a,b; a = 4; b = ++a + a++; …
Harri
  • 39
  • 3
3
votes
6 answers

C - Incrementation not updating variable value

I am working on a simple C program, but ran in to some confusion. Below is the code: int main(void) { int i, j, k; i = 3; j = 4; k = 5; printf("%d ", i < j || ++j < k); printf("\n"); // LINE 1 printf("%d %d %d", i, j, k); // LINE…
sye
  • 201
  • 3
  • 4
  • 10
3
votes
1 answer

Bash operator ++ behavior weired with a constant

By occasionally, I meet a problem when calculating a self-increment operation ++ with a constant, the behavior is weird for me (this is not the original code, I just copied the looks-like lines). #!/bin/bash echo "out1="$((++5)) echo…
Hui
  • 127
  • 1
  • 9
3
votes
2 answers

How to overload the ++ operator for a enum in C++

This is what I tried, but I see that overloading only increments the variable if I assign it to another variable. I.e, The value of the variable on which I do the increment does not increase. So, in the example below variable newDay is THU but…
Arvind Swami
  • 83
  • 1
  • 7
3
votes
5 answers

How does this code work?

I am looking at c++ for dummies and found this code #include #include #include using namespace std; int nextStudentId = 1000; // first legal Student ID class StudentId { public: StudentId() { value =…
user451498
3
votes
3 answers

c++ spaces in operators , what are the rules

Does spaces have any meaning in these expressions: assume: int a = 1; int b = 2; 1) int c = a++ +b; Or, 2) int c = a+ ++b; When I run these two in visual studio, I get different results. Is that the correct behavior, and what does the…
user3599803
  • 6,435
  • 17
  • 69
  • 130