pre-increment

What are the historical reasons C languages have pre-increments and post-increments?

半世苍凉 提交于 2019-12-03 06:16:41
(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement operators for very good reasons. What I don't understand is why they decided to create the distinction of pre- vs post- increments/decrements? My sense is that these operators were far more useful when C was being developed than today. Most C/C++ programmers use one or the other, and programmers from other languages find the distinction today bizarre and

How is the postfix and prefix increment operator evaluated in an expression? [duplicate]

百般思念 提交于 2019-12-02 23:38:56
问题 This question already has answers here : Undefined behavior and sequence points (5 answers) Closed 6 years ago . #include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; int main() { int n = 5; cout<< n++ <<" "<< ++n << " "<< n++; _getch(); return 0; } When I run this program on Visual Studio the output is 7 8 5. I think it is compiler dependent. ( Correct me if I am wrong) But shouldn't it be either 7 7 5 or 5 7 7 ? 回答1: The sequence, in which the various n++ / ++n are

How is the postfix and prefix increment operator evaluated in an expression? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-02 13:36:58
This question already has an answer here: Undefined behavior and sequence points 5 answers #include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; int main() { int n = 5; cout<< n++ <<" "<< ++n << " "<< n++; _getch(); return 0; } When I run this program on Visual Studio the output is 7 8 5. I think it is compiler dependent. ( Correct me if I am wrong) But shouldn't it be either 7 7 5 or 5 7 7 ? The sequence, in which the various n++ / ++n are executed, is undefined by the C standard and might change over time or depending on target machine and/or optimization options.

Semantics of pre- and postfix “++” operator in Java [duplicate]

≡放荡痞女 提交于 2019-12-02 02:09:00
This question already has an answer here: Java: Prefix/postfix of increment/decrement operators? 10 answers I wondering to know why this snippet of code give output 112 How this last digit 2 was creating? public static void main(String[] args) { int i = 0; System.out.print(++i); System.out.print(i++); System.out.print(i); Why does this happen? Your snippet it's translated as int i = 0; i = i + 1; // 1 System.out.print(i); // 1 System.out.print(i); // 1 i = i + 1; // 2 System.out.print(i); // 2 That's why the final result it's a 2. ++i it's incrementing the variable before being called by the

whether a language needs preIncrement (++x) and postIncrement (x++)

雨燕双飞 提交于 2019-12-02 00:50:33
问题 I have never seen the usecase for pre-increment and post-increment in actual code. The only place i see them most often are puzzles. My opinion is, it introduces more confusion rather than being useful. is there any real use case scenario for this can't this can be done by using += y = x++ y = x x += 1 回答1: It's just a shorter way of writing the same thing and it's only confusing to those who don't deeply understand C (a) . The same argument could be made for replacing: for (i = 0; i < 10; i+

whether a language needs preIncrement (++x) and postIncrement (x++)

五迷三道 提交于 2019-12-01 21:14:21
I have never seen the usecase for pre-increment and post-increment in actual code. The only place i see them most often are puzzles. My opinion is, it introduces more confusion rather than being useful. is there any real use case scenario for this can't this can be done by using += y = x++ y = x x += 1 It's just a shorter way of writing the same thing and it's only confusing to those who don't deeply understand C (a) . The same argument could be made for replacing: for (i = 0; i < 10; i++) printf ("%d\n", i); with: i = 0; while (i < 10) { printf ("%d\n", i); i = i + 1; } since any for can also

Is this an undefined behavior in C? [duplicate]

♀尐吖头ヾ 提交于 2019-12-01 13:26:29
问题 This question already has answers here : Undefined behavior and sequence points (5 answers) Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 2 years ago . 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 displays 14 14 14 14 . Can someone please explain

Confusion about an error: lvalue required as unary '&' operand [duplicate]

百般思念 提交于 2019-12-01 06:03:53
This question already has an answer here: Not able to understand error condition wrt lvalues 3 answers I have been trying to understand pointer concepts by writing simple code, and I got an error problem, and it seems like I couldn't solve it or understand it. #include <stdio.h> int *foo(void); int main(void) { printf("%d\n", *foo()); return 0; } int *foo(void) { static int num = 1; ++num; return &(++num); } Here is the error message. error: lvalue required as unary ‘&’ operand return &(++num); Function 'foo()' returns a pointer to int, and main is supposed to be print the returned int by

Why is i=i+1 faster than i++?

允我心安 提交于 2019-12-01 04:26:27
问题 Test this code in Flash: var i:int = 0; for (var j:int = 0; j < 5000000; j++) { i=i+1; }// use about 300ms. i = 0; for (var j:int = 0; j < 5000000; j++) { i++; }// use about 400ms i = 0; for (var j:int = 0; j < 5000000; j++) { ++i; }// use about 400ms too Why is i=i+1 faster in ActionScript 3 when it's slower in others? Sorry,I make some mistake.The code above use the same time. but if put it into function,and the result will be different. var i:int; var j:int; var startTime:Number; function

Confusion about an error: lvalue required as unary '&' operand [duplicate]

浪子不回头ぞ 提交于 2019-12-01 03:34:24
问题 This question already has answers here : Not able to understand error condition wrt lvalues (3 answers) Closed 5 months ago . I have been trying to understand pointer concepts by writing simple code, and I got an error problem, and it seems like I couldn't solve it or understand it. #include <stdio.h> int *foo(void); int main(void) { printf("%d\n", *foo()); return 0; } int *foo(void) { static int num = 1; ++num; return &(++num); } Here is the error message. error: lvalue required as unary ‘&’