There are several ways of incrementing in the C language.
Often, I might need to increment a number, and so I might do something like this:
i = i + 1
In fact, I might (I will) do it so often that there is a nice short hand.
i += 1.
This does the exact same thing as i = i + 1, just requires less typing.
It not only works for addition, but also subtraction, multiplication, division, and even logical operators.
Let's say I want to make a power function that allows me to calculate expressions like 3^4:
int power(int base, int exponent)
{
. . . . int result = 1;
. . . . while (exponent > 0)
. . . . {
. . . . . . . . result *= base;
. . . . . . . . exponent = exponent - 1;
. . . . }
. . . . return result;
}
What are powers in mathematics? They are just repeated multiplication.
So, for 3^4.
We start with the variable result, which eventually hold the answer. But it starts at 1.
Multiply that by 3, and keep the product stored in result. So far, result = 3.
Again, multiply by 3, and store in result: so far, result = 9
and then result = 27
and result = 81.
result *= base is the same as saying result = result * base.
Notice that the power function shown above also decrements exponent using the same kind of operator, except this time subtraction:
exponent = exponent - 1.
I could have said exponent -= 1, which would be equivalent.
Furthermore, I could have said
exponent--;
or
--exponent;
These last to options are decrements (subtracting by 1). These decrements correspond to the
following increments:
exponent++
or
++exponent.
But what is the difference between doing ++i and i++.
Both these expressions modify i in a larger expression. ++i increments i first, and then i is used in the larger expression. With i++, i is used first, and then it is incremented
For example, suppose I have
i = 0;
printf("The value of i is %d\n", ++i); // i is incremented first, then used in the printf statement.
[Output: The value of i is 1.]
// continuing the same program:
printf("The value of i is %d\n", i++); // i is used in the printf statement and then incremented
[Output: The value of i is 1.]
The operator used as ++i is called the pre-increment operator (pre means before, and it increments the variable before the variable is used), and i++ is called the post increment operator.
Similarly, --i and i-- are called, respectively, the pre decrement and post-decrement operators.
This ability to specify exactly when a variable is incremented or decremented can come in handy. Take the power function from earlier:
int power(int base, int exponent)
{
. . . . int result = 1;
. . . . while (exponent > 0)
. . . . {
. . . . . . . . result *= base;
. . . . . . . . exponent = exponent - 1;
. . . . }
. . . . return result;
}
We can use the decrement operator on exponent:
int power(int base, int exponent)
{
. . . . int result = 1;
. . . . while (exponent > 0)
. . . . {
. . . . . . . . result *= base;
. . . . . . . . exponent--; // changed line
. . . . }
. . . . return result;
}
Or we can do
int power(int base, int exponent)
{
. . . . int result = 1;
. . . . while (exponent > 0)
. . . . {
. . . . . . . . result *= base;
. . . . . . . . --exponent; // same as exponent-- in this context;
. . . . }
. . . . return result;
}
In the above two examples, exponent-- and --exponent both make the function work because exponent is not part of a broader statement. That is, the timing of the increment does not affect any statements.
However we can make the function simpler by decrementing exponent immediately *after* the comparison exponent > 0: by using the post-decrement operator:
int power(int base, int exponent)
{
. . . . int result = 1;
. . . . while (exponent-- > 0) // can't do --exponent because it will reduce the number of loops by 1.
. . . . {
. . . . . . . . result *= base;
. . . . }
. . . . return result;
}
So, based on the above explanation, in your question 2, the answer is B, not C.