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
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 1 year ago.
Improve this question
In this piece of code:
int main()
{
int i=0;
while(i<10)
printf("%d",i++)
return 0;
}
If I am not wrong, the printf will always print the value +1 of i, so the first print will not be 0 but actually 1, am I right?
You are wrong. The post-increment i++ is evaluated to the value of i before incrementing, so what is printed in the first iteration is 0.
The expression x++ evaluates to x before incrementing.
int x = 5;
printf("%d\n", x++);
// Output: 5
printf("%d", x);
// Output: 6
On the other hand, ++x evaluates to x + 1, or x after incrementing:
int x = 5;
printf("%d\n", ++x);
// Output: 6
printf("%d", x);
// Output: 6
When you compile code it is read from left to right like text,
so for example your line 6
printf("%d",i++)
would first print the decimal and then add to variable.
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
#include<stdio.h>
int main() {
int x = 99;
printf("%d %d\n",1 > scanf("%d",&x) ? scanf("%d",&x): scanf("%d",&x),x);
}
So What is happening to the scanned values.
Lets say First Input is 11. so left most scanf is returning 1. So according to the rule the right most scanf will be executed. But The right most %d of printf is not printing the scanned value. It is showing 99.
You have unidefined behavior.
That is because you are modifying a value and accessing that value without an intervening sequence point. A simplified example with the same problem:
int x, y;
x = 1;
y = scanf("%d", &x) + x; /* <--- UB! */
Or also:
int x = 1;
printf("%d %d", x = 11, x); /* <--- UB! */
Note that while the ?: does insert a sequence point just after the condition, the comma that separates arguments in a function call does not. This measn, not only that the order of evaluation of function arguments is unspecified, but also that you must not modify the same value twice in the arguments of a function, or modify and use the same argument.
For example this other example is also UB:
int x = 0;
printf("%d %d", x++, x); /* <--- UB! */
Or this one:
int x[10] = {0}, i = 0;
printf("%d %d", x[i++], x[i++]); /* <--- UB! */
The solution for your problem is to separate the code in nice orderly sentences. A full sentence always ends with a sequence point!
int x = 99;
int y = 1 > scanf("%d",&x) ? scanf("%d",&x): scanf("%d",&x); /* ARGH! */
printf("%d %d\n", y, x);
Now, the line marked with the exclamation is quite nonsensical, but at least it is UB-free.
Function arguments may be evaluated in any order. In this case, it looks like x evaluates to 99 before scanf is called. The order of function argument evaluation is unspecified in C. This is C FAQ #3.7:
The comma operator does guarantee left-to-right evaluation, but the commas separating the arguments in a function call are not comma operators. [...] The order of evaluation of the arguments to a function call is unspecified.
Because there is no sequence point between the scanf that modifies x and the function argument that evaluates it, you are invoking undefined behavior, as explained in rodrigo's answer.
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.