Need operator increase / decrease in C

I doubt using Increment/Decrement Operators ( ++ and -- ) in C (Also in C++ ). Do I need to use ++ and -- in programs? For example, consider the code below,

 int x=10; x++; 

This can be easily replaced as follows:

 int x=10; x=x+1; 

So, is it necessary to use ++ and -- in practical programming? I ask this for a specific reason. In my opinion, they are one of the most complicated operators in C , forming expressions like *x++ , ++*x , x++ + ++x , etc. And causing thousands of mistakes every day.

Of course, I know that direct comparison between ++ and INCR assembly instruction is possible. But I believe that any decent compiler with some ability to optimize can replace x=x+1 with INCR x .

In short, my Question is - Is there a situation where x = x + 1 cannot replace x ++? "

It can be very useful if someone can provide a piece of code that will not work in the absence of ++ or -- .

Any suggestions? Thanks.

+6
source share
7 answers

Is there a situation where x = x + 1 cannot replace x++ ?

Of course, foo(x = x + 1) is different from foo(x++) because x = x + 1 gives the new value x and x++ gives the old value x .


In addition, there is not much difference, and strictly ++ and -- not needed (i.e. you can write all programs with x += 1 instead of ++x and x -= 1 instead of --x ).

Please note that in C ++ operator++ and operator-- can be overloaded and are especially useful when working with iterators.

+11
source

The main advantage of shorthand operators (++, -, + =, - =, * =, etc.) is that you do not need to repeat the operand. It does not really matter for

 x = x + 1; 

It is fairly easy to see that x is the same variable as x . But what about:

 ptr->kerflunk->var.arr[x+y+z*w+i] = ptr->kerflunk->var.arr[x+y+z*w+i] + 1; 

how different from:

 ptr->kerflunk->var.arr[x+y+z*w+i] = ptr->kerflunk->var.arr[x+yz*w+i] + 1; 

If you see:

 ptr->kerflunk->var.arr[x+y+z*w+i]++; 

In this latter case, it is very easy to see what is the same.

(And yes, I am doing an extreme example that can be improved by using some temporary variables for some bits of complex structure, etc., but the fact remains: an expression using ++ is much simpler, see "it increases this value" )

I don’t think that C and C ++ are good beginner languages, and it’s great to teach the early part of C and C ++ without using ++ , -- and += , -= etc. Just use x = x + 1 .

[And of course, if you like, you can still use C / C ++ to mess things up and still be what can be compiled: a[x = x + 1] = b[x = x + 1]; syntactically correct, but will also have the same problem as when using x++ twice between two points in a sequence].

+4
source

iterators.

x = x + 1 means that any number can be added to x, which may not be for some types of x.

In the case of iterators.

Forward iterators can only be increased, bidirectional iterators can also be reduced, but you cannot add any number to them. From a programmer’s point of view, it can provide efficient access to containers, so you don’t want to use std::list::begin() + 65536 or something like that.

A random access iterator , on the other hand, allows arithmetic operations with iterators.

- edit -

It is absolutely necessary.

No language feature is absolutely necessary.

In the case of C ++, you can get rid of stl, namespaces, classes, preprocessor, templates, local variables, exceptions and function calls, and STILL - create a working program that will do something. Of course, this program will be mostly unreadable, and it will take more time, but it is possible. Basically, all you need for programming is goto, arrays, global variables, a function that will read / write characters from / to the terminal, and what it is. Everything else is technically optional .

Language features are made to make your life easier, and not because it is impossible to live without htme. As for ++ / - operators, they are apparently most useful for implementing forward / bidirectional iterators and other data types that support previous / next semantics, but do not support increment / decrement with an arbitrary argument.

+3
source

It seems quite doubtful that this is absolutely necessary - there were many programming languages ​​(FORTRAN, COBOL, Pascal, PL / I, ALGOL, Simula and many others) that did not include a direct analogue of C pre - / post-increment / decrement.

However, there is one rather fundamental difference between ++x; or x++; and x = x + 1; - the latter evaluates the operand twice, where the former evaluates the operand only once.

This will make a difference in the case of a macro in which you pass an argument that has side effects, and you want these side effects to occur once rather than twice. However, there are not many real examples of this, because the most obvious examples of the argument that you only want to evaluate once (for example, where the argument itself is something like x++ ) simply will not compile if you apply ++ to them ( e.g. ++x++ ).

This is slightly weakened in C ++, but by the fact that (for example) a function can return a link. Wanting to call a function only once, incrementing the result that it returns is at least a reasonable possibility. Yes, you could do other ways (for example, most C ++ programmers would prefer the built-in function for the macro), but this is at least an opportunity.

+1
source

This is absolutely unnecessary ... but remember that C is not a computer language, it is a programming language, but still human ... C is perceived as a low-level language, but there is still one more possible output that the compiler could produce from :

i ++

it can store the value in a register, then increment, and then copy it back onto the stack, reduce that register for the next statement that relies on i to incr, and use the same register the next time it uses that value.

he could potentially use one mnemonics in the CIS to increase it on the stack.

he can make 2 copies, one to increase and one to return ...

It doesn’t matter, since C distracted him from you, but he is sure that the code is becoming more readable.

+1
source

For C ++, this is more of an efficiency than an opportunity, suppose you have an object x type x , which is an object of type class

 x = x + 1; 

this includes (x+1) , which adds 1 to x and returns a new copy of class x . Then this new temporary value x copied to x . This can be done without any copies using

 ++x; 

For C only thing I can think of is convenience . If you use it correctly, it is not confusing. Given the fact that unit increments and decrements are important parts of many algorithms, this helps to make the intention clear and not confuse it with “normal” addition or subtraction.

0
source

The short answer is that x = x + 1 cannot always be directly replaced with x++ (in terms of the search and replace operations), but any x++ appearance can be replaced with equivalent code that does not use the increment operator.

As written on the right side, a call to f(x = x + 1) conveys the incremental value of x , while a call to f(x++) fails; but you can easily fix it with f(x); x = x + 1; f(x); x = x + 1; .
You can even use x = (f(x), x + 1); although it may be harder to read.

The increment operator makes it easy to assign and increase a value on a single line, but you can choose whether the increment occurs before or after using the value:

 x = 1; y = ++x; // y = 2, x = 2 z = x++; // z = 2, x = 3 

With a simple assignment such as x = x + 1 , the value is always used after the increment occurs.

This is especially useful in pointer operations. For example, you can copy a zero-terminated string very briefly:

 while ((*dest++ = *src++) != '\0') ; 

This is a very common idiom, and experienced programmers will immediately recognize and understand it. So this is a very convenient shorthand, but you can always extend the code:

 while (true) { *dest = *src; if (*dest == '\0') { break; } src + src + 1; dest + dest + 1; } 

Although this gives the same result, much more effort is required for reading and understanding. In fact, I would expect an experienced programmer to wonder why the author didn’t just use a simpler version - was it inexperienced, or is there some kind of hidden difference?

0
source

Source: https://habr.com/ru/post/945362/


All Articles