Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Say I'm assigning codes to ASCII chars, where code 1 is assigned to the ASCII value 0, the NUL char, and so on. I'm doing this iteratively, currently with i+++1, that is:
for (int i = 0; i < 256; i++) {
// assign code i++ +1
arr[i].code = i++ +1;
}
If I were to use ++i instead, would that suffice?
This might help
y = i+++x -> Add x to current value of i and assign the result to y and then increment value of i
y = ++1 -> increment i by 1 and assign the result to y
I would prefer to write that i++ + 1. But yes, ++i has the same effect and value asi+++1.
The latter logically does more work to arrive at the result, though: it keeps the old value ofi, increments i, adds one to the old value and yields the sum as result. The preincrement operator just increments i and uses its new value as result.
Technically both should compile to basically the same code given an optimising compiler.
Yes. They are the same.
++ bounds tighter than + so the expression is evaluated from left to right as
s = (i++) + 1
which can further be expanded as
s = i + 1
i = i + 1
On the other hand, s = ++i does the same task. It can be expanded as
i = i + 1
s = i
It is preferred that you use the second version because it is more readable and extensible. But I don't think there will be a difference in speed.
Yes, i+++1 and ++i will both evaluate to the value of i plus 1, and will both leave i incremented by 1.
It's an odd way to do it, is there something wrong with just doing:
for (int i = 1; i < 256; i += 2)
Anyone reading your code later would prefer the latter, I believe.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If you had the following code:
j=3; // Line 1
i=6; // Line 2
i+=5; // Line 3
j=i--; // Line 4
... to my knowledge, the value of j would become 6. If it was --i, it would be 5 etc.
But if I print out the value of i after line 4, I get 10 instead of 11 which is on line 3.
Why does this happen? Does this mean if I declared i to be a number and later on I go back the code and set another variable to i--, before most of my i's in the code, it changes the value of i globally?
In your code, see the following step-through.
j=3; //Line 1, j ==3
i=6; //Line 2, i == 6
i+=5; //Line 3, i == i + 5 == 11
j=i--; // line 4, j == 11, i == 10, after this line.
To elaborate, the x += y can be broken down as x = x + y, so that's it.
and regarding the post-decrement, the side-effect (decrement) will take place after the expression is evaluated. So, anyway, before the next statement, the value of i will get decremented.
To add some reference, from C11, chapter §6.5.2.4,
The result of the postfix ++ operator is the value of the operand. As a side effect, the
value of the operand object is incremented (that is, the value 1 of the appropriate type is
added to it).[....]
and
The postfix -- operator is analogous to the postfix ++ operator, except that the value of
the operand is decremented (that is, the value 1 of the appropriate type is subtracted from
it).
Note, a difference of a pre-decrement and post-decrement is visible only within the expression they are used. From the perspective of the next instruction using the variable, they both will give you the same result (effect).
First you make the value of i become 11, then you assign this value to j and then the decrement of i happens. The last line is equivalent to
j = i;
i -= 1;
If you did j = --i; then the last line would have been equivalent to
i -= 1;
j = i;
and j would have become 10.
The -- decrement operators are not the same as subtraction by one. They modify the lvalue on which they were used, so any time i-- is used, i will be modified.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
There are actually two ways i can do this. One is to use the increment operator ++ and decrement operator –. For example, the statement “x++” means to increment the value of x by 1. Likewise, the statement “x –” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1″.
But why am i incrementing like this in my code and what does it mean?
for(i=0; i < numberOfProducts; ++i){
printf("Enter Product Name: ");
scanf("%s", &(pProducts+i)->productName);
printf("Enter Product Price: ");
scanf("%f", &(pProducts+i)->price);
}
My question is why did i use ++i for it to work? i tried i++ but could not print.
for(i=0; i < numberOfProducts; ++i)
and
for(i=0; i < numberOfProducts; i++)
are both equivalent as you are not reading the result of the operation (like in a = i++ vs a = ++i). The latter form is more common.
If you have different results, you probably have issues in the way you are testing your program.
++i is a pre-increment operation, meaning that i is first incremented then the incremented value is used in an expression. i++ is a post-increment operation, meaning that the existing value of i is first used in an expression, then it is incremented.
For example:
i=3;
x = 6 - i++;
printf("x=%d\n",x);
Outputs 3.
i=3;
x = 6 - ++i;
printf("x=%d\n",x);
Outputs 2.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main(void) {
int numPeople = 0;
numPeople = 10;
/* Your solution goes here */
if (numPeople = 10)
numPeople + 5;
printf("There are %d people.\n", numPeople);
return 0;
}
Write a statement that increases numPeople by 5.
numPeople += 5;
If numPeople is initially 10, then numPeople becomes 15.
Correct. Increasing 10 by 5 yields 15. The statement above satisfies this test-case.
You do a assignment in your if statement! So change it to this:
if (numPeople == 10)
//^^ See here 2x '=' is a comparison and 2x '=' is a assignment
Also you forgot the assign operator here:
numPeople + 5;
Change it to:
numPeople += 5; //Same as numPeople = numPeople + 5;
FYI:
You can initialize your variable with 10 like:
int numPeople = 10;
Also if you change your condition to this:
if (10 == numPeople)
It's a bit more save, because if you now make a typo and write = you will get a error and see it! The other way around it will just assign the value to the variable and it's hard to stop that then.
This is the assignment operator
if (numPeople = 10)
you need the comparison operator ==
if (numPeople == 10)
since the value of an assignment can be used as a condition, thus the code compiled correctly if you want to prevent such problem try this way
if (10 == numPeople)
if you erroneously use the assignment operator in this case, the code will not compile.
As it was, when reaching the if line, you was assigning 10 to numPeople and the next expression was doing nothing, so the value of numPeople was always 10.
To increment numPeople by 5 you need the += operator, or the following expression which is very easy to understand
numPeople = numPeople + 5;
so you assign to numPeople it's current value +5, you could also write this using the folloing
numPeople += 5;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was asked the output of following expression :-
I=10;
I++;
I++;
I++;
What will be the value of I at the end of this code. As per my knowledge, post increment in c means that first perform any other task like assignment ,printf etc and after it incteement the value of variable. Similarly in this case , first I should be 10, then I should be 10 and then it would be 11. But final answer came out to be 10 . how ?
The awnser should be 13.
int i = 10;
i++;
i++;
i++;
printf("%d", i);
test it urself
It would have been a nine-line program to demonstrate what happens to I in your question. In an expression containing I++;, I is used then incremented. In your example, since I is not used in any statement, you might have well used ++I to pre-increment it. But if a statement uses or tests I more than once, or contains a part that might not be executed, you must increment I afterwards.
#include<stdio.h>
int main() {
int I = 10;
I++;
I++;
I++;
printf ("%d\n", I); // prints 13
if (1 || I++) // I++ is not executed
printf ("%d\n", I); // prints 13
if (1 && I++) // I++ is executed
printf ("%d\n", I); // prints 14
return 0;
}
lets create an int i = 10;
i++;
is essentially the same as:
i = i + 1;
There is a slight difference though:
If you printf("%d", i++);
the printed value will be 10 since a ++ postfix will increment the value only after the value is used and i will equal 11 only on the next line, when:
printf("%d", i + 1);
will print 11 since it will be calculated before printf runs and i it self wont be changed since we didn't assign a value to it, we only used it to calculate a new value.
If you want behavior exactly the same as i = i + 1; you can use a ++ prefix like:
printf("%d", ++i);
in which case 11 will be printed and the value of i will increment by 1.
In your code you increment i using a ++ postfix 3 times without actually using i so all the code dose is increments i by one, there times. So at the end of the code i is equal to 13.
You can find more information on operators here:
http://www.tutorialspoint.com/cprogramming/c_operators.htm
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have this habit in C (and many other languages) where rather than foo <= bar I will do foo < bar + 1 and no idea where this even came from...
Is this bad, per se, or just nonstandard? I mean from the context of coding and later modifying the code...I assume any good compiler compiles both of them the same.
This is bad for multiple reasons:
Does not work for floating point numbers
Does not work for signed numbers (-2.5 <= -3 is false, but -2.5 < -3 + 1 is true)
Makes your code difficult to understand
Increases the chances that you'll (needlessly) create an overflow error
It's bad and nonstandard. There's really no reason to continue reinforcing the habit - you're just shooting yourself in the foot for later in your coding career.
I think it's less clear, personally. Also it could be bad if foo and bar are not integers; for example the two will mean something different if foo= -7 and bar = -7.5.
Based on your comment:
it helps prevent errors with how many iterations of loops take place
You're most likely referring to looping in these two ways:
Method I:
for(int i = 0; i < length; i++){ // } which would go between 0 and length-1.
Versus:
Method II:
for(int i = 0; i <= length-1; i++){ //} which also goes between 0 and length-1.
In these particular cases, either will work, but as Derek Redfern outlined in his answer above, don't make your code any more complicated than it should be.
It is very broken, especially when you do
int foo = <whatever>;
int bar = INT_MAX;
if (foo < bar + 1) {
/* guaranteed to never be called */
}
Also, it is likely to break if you "adjust" it in the other way, like so
int foo = INT_MIN;
int bar = <whatever>;
if (foo - 1 < bar ) {
/* again guaranteed never to be called */
}
In short, far better to check for equal and greater (or equal and less than) than to create code that adjusts a value during the comparison.