Use of increments in C

3

What is the difference in the use of increments in language C, considering an integer N?

N++
++N
N--
--N
    
asked by anonymous 22.02.2016 / 23:34

3 answers

4

The expression ++n increments the value and then returns the incremented value.

And in the expression n++ first returns the value of n and then the value is incremented. It is a subtle difference and the copier works the same way for the n-- or --n .

In a loop for you can use ++n , it's slightly faster. Already, n++ in a loop will create an extra copy of its value that will be thrown away, but that's no reason not to use n++ in loop for >, unless you are programming for hardware where memory is extremely limited, so it would be justifiable to use ++n in loop for .

Source: link

    
23.02.2016 / 00:47
1

The increment has the variation for each case that you want to use. ex:

int x = 10;
int y = 10;
printf("%i",x++); // imprime 10
printf("%i",x);  // imprime 11
printf("%i",++y); // imprime 11
printf("%i",y); // imprime 11

The increment or decrement after the variable, returns the variable before changing the value, before the variable, makes the change and then returns.

    
23.02.2016 / 00:26
1

These are the concepts of pre and post increment. And the same goes for decrement as well.

Being:

y = 0;
x = 10;

Pre increment

y = ++x;
y = 11;

Equivalent to the following assignment:

x++;
y = x;

That is, it increments x and then assigns the new value of x to y .

23.02.2016 / 01:23