Questions tagged [increment]

DO NOT USE THIS TAG ALONE. Use with a language tag like [javascript] or [python]. Adding one to the value of a variable, generally with the use of an increment operator.

Adding one to the value of a variable, generally with the use of an increment operator.

3520 questions
128
votes
5 answers

How to increment a pointer address and pointer's value?

Let us assume, int *p; int a = 100; p = &a; What will the following code actually do and how? p++; ++p; ++*p; ++(*p); ++*(p); *p++; (*p)++; *(p)++; *++p; *(++p); I know, this is kind of messy in terms of coding, but I want to know what will…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
118
votes
7 answers

How can I increment a char?

I'm new to Python, coming from Java and C. How can I increment a char? In Java or C, chars and ints are practically interchangeable, and in certain loops, it's very useful to me to be able to do increment chars, and index arrays by chars. How can I…
Tom R
  • 5,991
  • 9
  • 35
  • 41
116
votes
8 answers

The difference between ++Var and Var++

In programming, particularly in Java, what is the difference between: int var = 0; var++; and int var = 0; ++var; What repercussions would this have on a for loop? e.g. for (int i = 0; i < 10; i++) {} for (int i = 0; i < 10; ++i) {}
user559142
  • 12,279
  • 49
  • 116
  • 179
113
votes
8 answers

Why does c = ++(a+b) give compilation error?

After researching, I read that the increment operator requires the operand to have a modifiable data object: https://en.wikipedia.org/wiki/Increment_and_decrement_operators. From this I guess that it gives compilation error because (a+b) is a…
dng
  • 1,275
  • 1
  • 8
  • 10
108
votes
4 answers

bool operator ++ and --

Today while writing some Visual C++ code I have come across something which has surprised me. It seems C++ supports ++ (increment) for bool, but not -- (decrement). It this just a random decision, or there is some reason behind this? This…
Suma
  • 33,181
  • 16
  • 123
  • 191
83
votes
2 answers

increment value of int being pointed to by pointer

I have an int pointer (i.e., int *count) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call: *count++; However, I am getting a build warning "expression result unused". I can: call *count +=…
joels
  • 7,249
  • 11
  • 53
  • 94
73
votes
10 answers

What is the difference between "++" and "+= 1 " operators?

In a loop in C++, I usually encounter situations to use ++ or +=1, but I can't tell their difference. For instance, if I have an integer int num = 0; and then in a loop I do: num ++; or num += 1; they both increase the value of num, but what is…
E_learner
  • 3,512
  • 14
  • 57
  • 88
71
votes
2 answers

The difference between C and C++ regarding the ++ operator

I have been fooling around with some code and saw something that I don't understand the "why" of. int i = 6; int j; int *ptr = &i; int *ptr1 = &j j = i++; //now j == 6 and i == 7. Straightforward. What if you put the operator on the left side of…
Moe45673
  • 854
  • 10
  • 20
67
votes
9 answers

Increment (++) operator in Scala

Is there any reason for Scala not support the ++ operator to increment primitive types by default? For example, you can not write: var i=0 i++ Thanks
adelarsq
  • 3,718
  • 4
  • 37
  • 47
63
votes
8 answers

Is the += operator thread-safe in Python?

I want to create a non-thread-safe chunk of code for experimentation, and those are the functions that 2 threads are going to call. c = 0 def increment(): c += 1 def decrement(): c -= 1 Is this code thread safe? If not, may I understand why…
nubela
  • 1
  • 24
  • 75
  • 123
60
votes
3 answers

Interesting interview exercise result: return, post increment and ref behavior

Here's a simple console application code, which returns a result I do not understand completely. Try to think whether it outputs 0, 1 or 2 in console: using System; namespace ConsoleApplication { class Program { static void Main() …
Aremyst
  • 1,480
  • 2
  • 19
  • 33
54
votes
2 answers

INC instruction vs ADD 1: Does it matter?

From Ira Baxter answer on, Why do the INC and DEC instructions not affect the Carry Flag (CF)? Mostly, I stay away from INC and DEC now, because they do partial condition code updates, and this can cause funny stalls in the pipeline, and ADD/SUB…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
51
votes
2 answers

Difference between i++ and (i)++ in C

int i = 3; int j = (i)++; vs int i = 3; int j = i ++; Is there a difference between how the above two cases are evaluated? Is the first case equivalent to incrementing an rvalue or is it undefined behaviour?
Polaris000
  • 938
  • 1
  • 11
  • 24
49
votes
12 answers

JavaScript Calculate brighter colour

I have a colour value in JS as a string #ff0000 How would I go about programatically calculating a brighter/lighter version of this colour, for example #ff4848, and be able to calculate the brightness via a percentage,…
Ozzy
  • 10,285
  • 26
  • 94
  • 138
49
votes
11 answers

Why is ++i considered an l-value, but i++ is not?

Why is ++i is l-value and i++ not?
yesraaj
  • 46,370
  • 69
  • 194
  • 251