post-increment

Question about post-increment operator

馋奶兔 提交于 2019-12-24 06:01:06
问题 Why does the following code int i = 1; System.out.print(i += i++); System.out.print(i); output 2 two times instead of 3 for the 2nd print? Could somebody please shed some light on it? Thanks. 回答1: If you realise that a++ works as follows (pseudocode): func ++(ref a) { int b=a; a=a+1; return b; } then it all makes sense. 回答2: It may seems like i should be 3 in the end. However, if you look into the statement more closely i += (i++) is equal to i = ( i + (i++) ) which is, in this case, 1+1. The

Question about post-increment operator

大憨熊 提交于 2019-12-24 06:00:07
问题 Why does the following code int i = 1; System.out.print(i += i++); System.out.print(i); output 2 two times instead of 3 for the 2nd print? Could somebody please shed some light on it? Thanks. 回答1: If you realise that a++ works as follows (pseudocode): func ++(ref a) { int b=a; a=a+1; return b; } then it all makes sense. 回答2: It may seems like i should be 3 in the end. However, if you look into the statement more closely i += (i++) is equal to i = ( i + (i++) ) which is, in this case, 1+1. The

Why incrementing int primitive in while loop does not loop forever

自古美人都是妖i 提交于 2019-12-24 03:27:42
问题 I have a following code sample int i = 1; while(i != 0) { i++; } I was expecting this to run in an infinite loop but it didn't. Then when I printed the value after while loop I got: i value is 0. Can any one let me know what exactly is happening? 回答1: I was expecting this to run in an infinite loop but it didn't. No, it wouldn't. Eventually the value will become 0 again. In particular, it will logically execute like this: 1 2 3 ... 2,147,483,646 2,147,483,647 -2,147,483,648 // Integer

Operator Precedence.. () and ++

白昼怎懂夜的黑 提交于 2019-12-23 04:06:23
问题 Salute.. I have an unusual problem. Here in this table in MSDN library we can see that precedence of () is higher than ++ (Pre-increment) . but when I run this code, it seems that precedence of ++(prefex) is higher: int main() { int a=3,b=2,x; x=++a + (a-b); cout<<"x= "<<x; return 0; } and the answer is : x=6 This happens with prefex ++ only and works as I expect with post-increment . Is there any reason? Regards.. int main() { int a=3,b=2,x; x=a++ + (a-b); cout<<"x= "<<x; return 0; } x=4 (I

C/C++ Post-increment by more than one

若如初见. 提交于 2019-12-22 05:26:24
问题 I'm reading bytes from a buffer. But sometimes what I'm reading is a word or longer. // assume buffer is of type unsigned char * read_ptr(buffer+(position++)) That's fine but how can I post-increment position by 2 or 4? There's no way I can get the += operator to post-increment, is there? Reason is, I have this big awful expression which I want to evaluate, while at the same time incrementing the position variable. I think I came up with my own solution. I'm pretty sure it works. Everyone's

The assignment to variable has no effect?

Deadly 提交于 2019-12-22 04:40:08
问题 When I do this: count = ++count; Why do i get the warning - The assignment to variable count has no effect ? This means that count is incremented and then assigned to itself or something else ? Is it the same as just ++count ? What happens in count = count++; ? Why don't I get a warning for this ? 回答1: count++ and ++count are both short for count=count+1 . The assignment is built in, so there's no point to assigning it again. The difference between count++ (also knows as postfix ) and ++count

How is *it++ valid for output iterators?

蓝咒 提交于 2019-12-21 07:34:44
问题 In example code, I often see code such as *it++ for output iterators. The expression *it++ makes a copy of it , increments it , and then returns the copy which is finally dereferenced. As I understand it, making a copy of an output iterator invalidates the source. But then the increment of it that is performed after creating the copy would be illegal, right? Is my understanding of output iterators flawed? 回答1: The expression *it++ does not (have to) make a copy of it, does not increment it,

Precedence and associativity of prefix and postfix in c

耗尽温柔 提交于 2019-12-19 10:08:16
问题 int main() { char arr[] = "geeksforgeeks"; char *ptr = arr; while(*ptr != '\0') ++*ptr++; printf("%s %s", arr, ptr); getchar(); return 0; } The statement inside while loop ++ptr++ behaves in a way that I don't understand. The post increment should have been evaluated first because of its high priority and the first output value should have been f(incrementing e). But that does not happen. To understand i changed the statement to ++*(ptr++) , so it may give the output what i expect

Why doesn't the post increment operator work on a method that returns an int?

让人想犯罪 __ 提交于 2019-12-17 17:47:07
问题 public void increment(){ int zero = 0; int oneA = zero++; // Compiles int oneB = 0++; // Doesn't compile int oneC = getInt()++; // Doesn't compile } private int getInt(){ return 0; } They are all int's, why won't B & C compile? Is it to do with the way ++ operator differs from = 0 + 1; ? Invalid argument to operation ++/-- 回答1: i++ is an assignment to a variable i . In your case, zero++ is an equivalent to zero = zero + 1 . So 0++ would mean 0 = 0 + 1 , which makes no sense, as well as getInt

Undefined Behavior of Postfix or Prefix Increment in Function Calls in C [duplicate]

醉酒当歌 提交于 2019-12-17 16:53:43
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed last year . I have seen in this site that prefix increment or postfix increment in a function call may cause undefined behavior. I have gone through one of those recently. The source code is something like this : #include <stdio.h> void call(int,int,int); int main() { int a=10; call(a,a++,++a); printf("****%d %d %d***_\n",a,a++,++a); return 0; } void call(int