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
0
votes
1 answer

Pre-increment operator with and without pointer in C

I am running gcc 7.4.0 on ubuntu 18.04. I compiled and ran the following code #include int main() { int *p; int a[2]; a[0] = 100; a[1] = 200; p = a; int b = 10; printf("%d\t%d", ++*p, *p); printf("\n%d\n", *p); …
Bukks
  • 408
  • 7
  • 16
0
votes
1 answer

Query regarding post increment and pre increment operator

If I have the following code: int n=5; if (n++>=6){ cout<<"Yay"; } else cout<<"No"; And I compare it with this: int n=5; if (++n>=6){ cout<<"Yay"; } else cout<<"No"; Why is the output different in both cases? Isn't n being…
0
votes
1 answer

Pre-Increment (++i ) and i + 1 in Reccursions. Why do they have different action ? StackOverFlow Mistake

In the Recursion when I write res += countNatNum(++len, sum + i, k, d); I have a StackOverFlow mistake. But when I change pre-increment on len + 1 res res += countNatNum(len + 1, sum + i, k, d); everything is OK. I don't understand why does it…
0
votes
2 answers

Result of function that includes pre- and post-increment differs

I had to understand some code which mixes pre- and post-increments in functions. There was one thing that confused me. So I tried to test some smaller function. But I could not explain following behaviour: int i = 1; i = i++ * ++i *…
tobsob
  • 602
  • 9
  • 22
0
votes
1 answer

What equivalent in Delphi of pre-increment (++i) in C++ For command?

Wha's equivalent in Delphi of this c++ sintaxe? See that i variable is incremented before. for(int i = 0; i < 20; ++i) Thanks in advance. EDIT: In my case, this is how ++i is used with sintaxe above: void testStruct *testMethod() { for(int i =…
FLASHCODER
  • 1
  • 7
  • 24
0
votes
3 answers

Why does exchanging variable name changes output of C program?

I tried this example of array and post increment/ pre increment on it's elements #include int main() { int j[5] = {5, 1, 15, 20, 25}; int k , l, m, n; n = ++j[1]; k = ++j[1]; l = j[1]++; m = j[k++]; …
0
votes
1 answer

What's the difference between while(--i); and while(i) --i;?

My first submission of 61.Rotate List ran for 16 ms and I was not happy with that. So I changed this part of my code k += 1; while (--k) { p = p->next; } to while (k) { p = p->next; --k; } and then magic happened. Runtime decreased to…
Liyin Shao
  • 25
  • 4
0
votes
6 answers

Confusion about for loop in c: increment logic in c language

I have a confusion in basic concept for C language for loop increment. This is my code: #include #include int main() { int j=0;int a; for(j;j<=3;++j) { printf("hello\n"); a=j; } …
piku_baba
  • 59
  • 7
0
votes
3 answers

Why does increment operation ++a++ not work, at least in C?

I was curious and find out the current piece of code does not work however I can not figure out why: #include void main(){ int a = 42; printf("%d\n", ++a++); } It does not seem like a wrong syntax to me. Could somebody…
curiouscupcake
  • 1,157
  • 13
  • 28
0
votes
1 answer

Why the first two variables (a, b) are pre-incremented but the third one is not(c)?

I am having an issue I cannot understand. The output for a is 6, b is -6 but the c remains the same no matter what variable I initialize it with. #include void main() { int a = 5, b = -7, c = 0, d; d = ++a && ++b ||…
MarcoM
  • 11
  • 2
0
votes
1 answer

Overloading operator without passing argument

I am trying to implement a time class which sets time, prints it and increases by one second. I want to make it by overloading the ++ operator, and it works fine but only when I define an argument in the parameter list. How can I make it work…
Lois2B
  • 111
  • 1
  • 16
0
votes
0 answers

Pre-increment and post-increment in C++ not working correctly when used as arguments

I came across this problem in c++ randomly. By my understanding this code: #include using namespace std; void foo(int a, int b){ cout<<"a = "<
Angeal
  • 1
  • 2
0
votes
1 answer

In what scenario will pre-increment be undefined behaviour in C?

In cases like: int q = 3; ++q * ++q It will be an undefined behaviour in C. However, what about the following scenarios? ++q * q ++q * q++ Note: I am not asking what is the definition of undefined behaviour. My question is: What are the specific…
user3437460
  • 17,253
  • 15
  • 58
  • 106
0
votes
1 answer

Is this an undefined behavior in C?

I am running my C code on gcc to understand pre/post increment operator. However the results that I see are not what I expected. Like for the line 6, since i is 5, it should have been 8 7 6 5 5 But it is 8 7 6 5 8 Then coming to the last line, it…
0
votes
0 answers

What is the purpose of using pre-increment vs post-increment if return value is not used?

I have see code that pre-increment (or decrement) of variables without return value to be used. ++i; vs i++; Example here : https://referencesource.microsoft.com/#mscorlib/system/threading/SemaphoreSlim.cs,809 Is there any purpose of doing this…
tigrou
  • 4,236
  • 5
  • 33
  • 59