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
3 answers

Lvalue issues in increment Operators

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) #include int main() { char a[]="Hello"; char *p=a; while(*p) ++*p++; //Statement 2 printf("%s",a); int x=10; …
Vignesh_dino
  • 313
  • 2
  • 6
1
vote
2 answers

Pre increment operator

#include using namespace std; int main() { int i=2; cout<<++i<<" "<<++i; return 0; } Why the output of program is '4 4' not '3 4' ?
Shubham Jaiswal
  • 15
  • 1
  • 1
  • 4
1
vote
3 answers

Dereferencing a preincremented pointer is giving odd result

This one must be a silly question, but I am not able to understand why this happens int main() { int i=20; int *p=&i; cout<<"old p="<
sajas
  • 1,599
  • 1
  • 17
  • 39
1
vote
2 answers

Can you have a incrementor and a decrementor on the same variable in the same statement in c

Is --foo++; a valid statement in C? (Will it compile/run) And is there any practical application for this? Sorry for changing the question in an edit but I found something out. According to my C++ compiler (Visual Studio 2010): --++foo; is a valid…
Ashterothi
  • 3,282
  • 1
  • 21
  • 35
1
vote
1 answer

SCJP program giving output 8 2 how?

class Foozit { public static void main(String[] args) { Integer x = 0; Integer y = 0; for (Short z = 0; z < 5; z++) { if ((++x > 2) || ++y > 2) x++; } System.out.println(x +…
1
vote
1 answer

Result of Boolean Expression in C

Why does the following expression evaluate to 0? i > --i Suppose i = 5. Evaluating the expression from left to right, we evaluate the left operand (i) to get 5 and we evaluate the right operand (--i) to get 4. So the expression about should…
JellalF
  • 115
  • 3
0
votes
0 answers

Can Overloaded pre increment and decrement, post increment and decrement operators not work well with inheritance?

Start with the COUNTEN2 program in this chapter. It can increment or decrement a counter, but only using prefix notation. Using inheritance, add the ability to use postfix notation for both incrementing and decrementing. (See Chapter 8 for a…
0
votes
0 answers

Explain the equation and its answer

#include using namespace std; int main() { // your code goes here int x=3; int y; y = x+ (--x) + (x++) + x + (++x); cout << y << endl; return 0; } The output of is 13. But it should be 14. Any explanation please…
Samiul
  • 1
0
votes
1 answer

Is i++ in any immaginable scenario faster than ++i?

(I'm assuming the old value of i is not needed, so i++ and ++i should at most affect performance.) The thing is, I've seen a counter incremented like this in code i++; and it left me wondering: should I suggest to write ++i; because given the…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
0 answers

how is 1st code working to get 21 while when same value is assigned to **b** the post increment operator doesn't work?

when running the below code the output is 21 #include int main() {int a=10,b=10; printf("%d",a+(a++)); } whereas running below code gives output 20 int main() {int a=10,b=10; printf("%d",b+(a++)); } how is 1st code working to get 21…
Noobcoder
  • 31
  • 7
0
votes
2 answers

Usage of '+=' in c

I don't understand why, on this code, 'b+=' return 6 instead of 5. The operation on right-end of operator '+=', should be 0. i/2 = 4 a-4= 0 So Operator '+=' should just add: 0. #include int main(){ int a=4, i=8; int b; b=++a; …
0
votes
5 answers

Why is a++=b disallowed, while c[i++]=d is permitted?

Why according to the standard is a++ = b; disallowed, while c[i++] = d; is permitted? (Obviously, a++ = b; would be bad style, but this is a question about a close reading of the C language standard.) Here is the obligatory minimal example: #include…
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
0
votes
0 answers

Different outputs on vs code and replit.com

I am trying to run this code which is written in C language. It is giving different outputs when run on VS code and Replit.com. I am confused. Please explain #include int main() { int x = 1, y = 6, z = 0; printf("\n x=%d \n z=%d \n…
0
votes
0 answers

Different Output through gcc and clang complier

so i compline this code in C #include int main() { int a = 1; printf("%d",a++); printf("%d",a); printf("%d",++a); int b =1; printf("\n%d%d%d",b++,b,++b); } out in gcc 123 233 out in clang 123 123
0
votes
0 answers

The following code giving answer 21. I am not understanding how the operators are working

#include int main() { int i = 10; int d = ++i + i--; printf ("d = %d\n",d); return 0; } This code printing d = 21. The order of evaluation is taken right to left and left to right, but still the answer is not evaluating to 21.
Moody
  • 111
  • 1