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
1
vote
1 answer

Is increment stackable? I.e x++++; or (x++)++;

When me and my friend were preparing for exam, my friend said that x+++; is the same as x+=3; It is not true but is x++++; same as x+=1; or is (x++)++;? Could I generalize it? I.e. x++++++++++++++; or ((((((x++)++)++)++)++)++)++; is equivalent to…
JRBros
  • 43
  • 6
1
vote
3 answers

Is formatted output equivalent?

When I was doing the practice questions today, I found that the outputs of printf("%d\n",x--); and printf("%d\n",x); are the same. I changed it to printf("%d\n",x++); and found to be the same. I want to know why. #include int main() { …
1
vote
2 answers

`++` and `+=` operator are behaving differently

I have a code which print pointer to const char, or I mean a string, recursively. My code works fine when I'm using += operator to call print() function. But, when I'm using ++ operator my code goes to an infinite loop just printing 'H'. Here's my…
user18326094
1
vote
0 answers

Internal behaviour of ++x as an lvalue in C++

I know in C++, you can have ++x as an lvalue and that it evaluates to x. But I ran the following code and am curious about what is going on behind the scenes. x = 10; ++x = x + 5; In this case the final value of x is now 15. So does cpp not try to…
1
vote
5 answers

Multiple increment operators in single statement

Possible Duplicate: Undefined Behavior and Sequence Points Pleae explain the behaviour of following statements int b=3; cout<
Kumar Alok
  • 2,512
  • 8
  • 26
  • 36
1
vote
2 answers

Unable to figure out the logic process to get this answer without running the program - C++

#include using namespace std; int fun1(int p){ ++p; return p++; } int fun2(int &p){ ++p; return p++; } int main(void){ int a = 1, b, c; b = fun1(a); c = fun2(b); cout<< a + b + c << endl; return 0; } The answer I get…
1
vote
1 answer

C++ Help: Error: lvalue required as increment operand

What is wrong here. What does it mean by the error: lvalue required as increment operand? (NOTE: this is a textbook example) #include using namespace std; int main() { int num1 = 0, num2 = 10, result; num1++; …
Cyrus Lee
  • 27
  • 4
1
vote
3 answers

Can anyone explain exactly what the post- and pre- increment is, because from what I know, I'm just getting more confused?

shouldn't this be, 11 is good and 21 is bad? because, in case of i++, the value 10 first gets evaluated if equal to i, and then incremented? int i = 10; if(i++ == i){ System.out.println(i + " is good"); } else{ System.out.println(i + " is…
Queenish01
  • 51
  • 4
1
vote
4 answers

Strange output when using do/while in C programming lang

I am trying to understand the output of this program. If I try to "translate" the code, I believe it should go like this: while "j" is smaller than 3 - print "Ha" (this loop goes 3 times, so it gives 3 "Ha") do/while -> j is equal to j - 2 hence…
Lynx
  • 11
  • 1
1
vote
3 answers

What is the difference between *++a and ++*b?

Here in the below program, the 'c' pointer has not been modified. Still, it's printing the second element of the array instead of the first one i.e. 'a'. Can anyone explain this behavior? The first two characters are printed correctly but the third…
Jay
  • 43
  • 6
1
vote
3 answers

How to do pre increment without using ++I?

I don't think that the following: i += 1 or i = i + 1 is the same as ++i. Or am I wrong? Is there any way to do pre increment without using ++ operator?
jxStackOverflow
  • 328
  • 1
  • 7
  • 17
1
vote
3 answers

Difference between s[++i]; and s[i]; ++i;

I'm doing some exercises from "C Programming Language" and can't figure out what is going on to give me certain output. Not really a roadblock because I got the output I wanted, but I don't understand why changing a certain piece of my code actually…
1
vote
3 answers

How many times could a variable be incremented in a single statement?

I am currently reading Barry Burd's Java For Dummies, and came upon this little exercise. The exercise is regarding post and pre-increment. In the problem (please see the code) I was able to figure out the answers to all the lines(without the help…
F.A.
  • 15
  • 5
1
vote
3 answers

How to appropriately use pointers in C++ functions?

I'm trying to get the hang of pointers and addresses in C++ and am having trouble with functions with changing parameters. The code below is writing Loop run #1. in an infinite loop, instead of incrementing the value foo. My question is: What is the…
peki
  • 669
  • 7
  • 24
1
vote
1 answer

Increment operator works different while running and debugging

Here is the portion of the code which works weird while debugging public static void main(String[] args) { //Line-1 int a = 5; //Line-2 System.out.println(++a); //Line-3 } //Line-4 When I run it in normal mode it gives output as 6 which is…
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52