I tried this example of array and post increment/ pre increment on it's elements
#include<stdio.h>
int main()
{
int j[5] = {5, 1, 15, 20, 25};
int k , l, m, n;
n = ++j[1];
k = ++j[1];
l = j[1]++;
m = j[k++];
printf("\n%d, %d, %d, %d", n, k, l, m );
return 0;
}
here the output is :
2, 4, 3, 20
and if i change the order of n and k
ie instead of
n = ++j[1];
k = ++j[1];
i write
k = ++j[1];
n = ++j[1];
The output becomes :
3, 3, 3, 15
I tried this on mingw compiler on windows10 and also on Kali Linux's GCC...
Same problem.
It is just like taking different variable name alters the output of program.
What might be the cause?
Thanks Everyone for helping me out with this question.
I didnt take the last post increment of k under consideration.
The results would be same of i would have changed
m=j[k++]
with
m = j[n++]
Why would not they? The values of k, n and m are depending on their placement in the code.
For example,
m = j[k++];
this will be affected by the current value of k.
To add a bit about pre and post increment operator, quoting C11,
Chapter §6.5.3.1, pre-increment
The value of the operand of the prefix ++ operator is incremented. The result is the new
value of the operand after incrementation. [...]
Chapter §6.5.2.4, post-increment
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). [....]
I have added some print statements, to check how the values get affected after every statement. You can additionally print the whole array to visualize the change even more.
int j[5] = {5, 1, 15, 20, 25};
int k , l, m, n;
printf("j[1] = %d\n", j[1]);
k = ++j[1];
printf("j[1] = %d, k = %d\n", j[1], k);
n = ++j[1];
printf("j[1] = %d, n = %d\n", j[1], n);
l = j[1]++;
printf("j[1] = %d, l = %d\n", j[1], l);
m = j[k++];
printf("m= %d, k = %d\n", m, k);
printf("\n%d, %d, %d, %d", n, k, l, m );
and the output is:
j[1] = 1
j[1] = 2, k = 2
j[1] = 3, n = 3
j[1] = 4, l = 3
m= 15, k = 3
3, 3, 3, 15
In the first case:
n = ++j[1]; //j[1] is incremented and is now 2, so n is 2
k = ++j[1]; //j[1] is incremented again and is now 3, so k is 3
l = j[1]++; //j[1] is incremented yet again and is now 4, but l is 3 as it is post-increment
m = j[k++]; //m is j[3] which is 20, and k will become 4 as it is post-incremented
So output of n, k, l, m will be 2, 4, 3, 20
In the second case:
k = ++j[1]; //j[1] is incremented and is now 2, so k is 2
n = ++j[1]; //j[1] is incremented again and is now 3, so n is 3
l = j[1]++; //j[1] is incremented again and is now 3, but l is 3
m = j[k++]; //m is j[2] which is 15, and k will become 3 as it is post-incremented
So output of n, k, l, m will be 3, 3, 3, 15
Actually, it is not that complicated
int j[5] = {5, 1, 15, 20, 25};
n = ++j[1];
// n=2 j[1]=2
k = ++j[1];
// k=3 j[1]=3
l = j[1]++;
// l=3 j[1]=4
m = j[k++];
// since k=3, k becomes 4 but m is assigned j[3] which is 20
Let's see the other case
int j[5] = {5, 1, 15, 20, 25};
k = ++j[1];
// k=2 j[1]=2
n = ++j[1];
// n=3 j[1]=3
l = j[1]++;
// l=3 j[1]=4
m = j[k++];
// since k=2, k becomes 3 but m is assigned j[2] which is 15
Related
This C program will take the value stored in the variable a and print them one by one.
#include <stdio.h>
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0)
return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d, ", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf("\n");
return 0;
}
Output:
2, 0, 4, 8,
When the function foo executes:
1) For the first time: n = 2048, k = 8, j = 204, sum = 8
2) For the second time: n = 204, k = 4, j = 20, sum = 12
3) For the third time: n = 20, k = 0, j = 2, sum = 12
4) For the fourth time: n = 2, k = 2, j = 0, sum = 14
If I replace the line (present in the foo function):
printf ("%d, ", k);
with this:
printf ("%d | %d, ", k, sum);
Output:
2 | 14, 0 | 12, 4 | 12, 8 | 8,
Can someone please explain how this program works:
1) How it's printing value stored in a?
2) And in this order: 2, 0, 4, 8, ?
3) Why is the value of sum is changing when we're printing values of k?
4) What would happen when n become 0?
You are calling the function foo on a.
That is the order since you are printing AFTER you are processing the rest of the number. Try moving the call to printf before the call to foo in foo and see if you get anything different.
sum is changing because you are doing sum = sum + k and passing it to all the future calls.
When n eventually becomes 0 due to repeated divisions, the last call to foo starts returning and following them all the previous calls start returning after printing the digit they had extracted using n % 10
I have this piece of C code
#include <stdio.h>
int main(){
int i , j , m , A[5]={0,1,15,25,20};
i = ++A[1];
printf("%d:\n",i);
j = A[1]++;
printf("%d:\n",j);
m = A[i++];
printf("%d:\n",m);
printf("%d %d %d",i,j,m);
return 0;
}
and It's output is
2:
2:
15:
3 2 15
Shouldn't the printf print the values as 2 , 2, 15 but why is it printing 3 , 2, 15
P.S : I really didn't abuse this code , someone else did (my professor perhaps) and I'm just learning C .
The line
m = A[i++];
will increment the variable i in-place after it gets the cooresponding value from the array A.
i is incremented as a part of below statement
m = A[i++];
Lets see what we got here..
int i , j , m , A[5]={0,1,15,25,20};
i = ++A[1]; // takes the value of A[1], increment it by 1 and assign it to i. now i = 2, A[1] = 2
printf("%d:\n",i);
j = A[1]++; // takes the value of A[1](which is 2), assign it to j and increment the value of A[1] by 1. now j = 2, A[1] = 3
printf("%d:\n",j);
//remember the value of i? its 2
m = A[i++]; // takes the value of A[2](which is 15), assign it to m and increment the value of i by 1. now m = 15, i = 3
printf("%d:\n",m);
printf("%d %d %d",i,j,m); // Hola! we solve the mystery of bermuda triangle :)
return 0;
m = A[i++];
this code assign A[2] which is 15 to the variable m ,and then +1 to the current value of i to become 3.
I recently did an exam and there was a bonus question and I was the only person who got it right. I am just as curious as much as my class mates are on how I got it right.
The question was:
Use this array: int a[5] = {5, 1, 15, 20, 25};
Use these variables: int i, j, m;
Use this base code:
i = /*Enter Code*/;
j = /*Enter Code*/;
m = /*Enter Code*/;
printf("%d, %d, %d", i, j, m);
Get this answer: 3, 2, 15
There were a bunch of conditions but the main was that we were only allowed to use the array and one '1' and one '++' per variable equals.
I got the answer right through trial and error but I have no idea how I got it right and how it is right. Here is my code:
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
_getch();
return 0;
}
I am purely curious about how I got the right answer. Any explanation would be great.
Thanks in advance.
So, just look at the three statements:
i = ++a[1];
This changes a[1] to 2, and assigns that value to i.
j = a[1]++;
This sets j to 2, and changes a[1] to 3.
m = a[i++];
This sets m to 15, and changes i to 3.
So the final values that are printed the required ones, 3, 2, 15.
If you ran this in a debugger, you could watch all the variables and see it happen statement by statement.
Like everyone said, just step through your code with a debugger, or manually :
at beginning
a[5] = {5, 1, 15, 20, 25}
after i = ++a[1];
a[5] = {5, 2, 15, 20, 25}
i = 2
because the pre-incrementation ++ will modify a[1] before affecting him to i.
after j = a[1]++;
a[5] = {5, 3, 15, 20, 25}
i = 2
j = 2
because the post-incrementation ++ will modify a[1] after affecting him to j.
after m = a[i++];
a[5] = {5, 3, 15, 20, 25}
i = 3
j = 2
m = 15
because the post incrementation will modify the value of i after the affectation to m, so at the time of affectation, i will be equal to 2, which a[2] = 15.
You need to understand the difference between ++a and a++ (and when the two incrementation will take place).
Shouldn't the output of following program be -
2 3 20
instead it is showing
3 2 15
Can anyone explain the reason behind this?
#include<stdio.h>
main()
{
int a[5] = {5,1,15,20,25};
int i,j,m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d %d %d",i,j,m);
}
3 2 15
is the correct output.
i is 3, because i became 2 in i = ++a[1]; for pre-increment and then it got post-incremented in m = a[i++];
j is 2, because j = a[1]++;, no changes afterwards.
m is 15 because m = a[i++]; i is being post-incremented, the old value of i (which is 2) is used in indexing and the post-increment on i is sequenced after the evaluation of the = statement.
Having said that, the recommended signature of main() is int main(int argc, char *argv[]) or at least, int main(void).
At this point, values of variables are:
a = {5,1,15,20,25};
i = uninitialized
j = uninitialized
m = uninitialized
Now,
i = ++a[1];
Gets the value of a[i] which is 1, increments it and it becomes 2, and then, it is stored in i.
At this point, values of variables are:
a = {5,2,15,20,25};
i = 2
j = uninitialized
m = uninitialized
Next,
j = a[1]++;
Gets the value in a[1] which is 2 (since it was incremented in the previous statement), stores this value in j and then, increments the value stored in a[1].
At this point, values of variables are:
a = {5,3,15,20,25};
i = 2
j = 2
m = uninitialized
Then,
m = a[i++];
Gets the value in a[i](a[2] since i is currently 2) which is 15 and this value is stored in m. Then, i is incremented.
At this point, values of variables are:
a = {5,3,15,20,25};
i = 3
j = 2
m = 15
3 2 15 is correct
#include<stdio.h>
main()
{
int a[5] = {5,1,15,20,25};
int i,j,m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d %d %d",i,j,m);
}
Now lets go line by line assume i , j , m equals 0 {better to initialize}
from line 3 , i = ++a[1];
i = 2 as (++ pre increment , change then use , and a[1] = 1 so , i = 2)
from line 4, j = a[1]++;
j = 2 as (++ here is post increment , use then change , a[1] becomes 3 but j is equals to 2)
from line 5, m = a[i++];
i = 2 by line 3 , here ++ post increment then i will increment to 3 but a[2] will be used .
Hence i = 3 , j = 2 , m = 15
Hope you got it ..........
This is a question from a practice test that I do not fully understand.
For the code fragment
int i = 0, j = 0, k = 0;
for (i=-1; i<=10; ++i){
j = i; ++k;
}
I am asked to find the values of the variables after executing the code.
The answers are:
i = 11 j = 10 k = 12
I don't understand how, can someone please help?
Understanding the value of i after the loop is very simple, much simpler than the sorts of other answers here. The loop condition is i<=10 ... in order for the loop to terminate, that condition must be false. Clearly, the value of i that makes that false is 11.
The value of j at the end of the loop is the previous value of i, which is 10, and the value of k is the number of times the loop executed, which is 1 (for -1) + 1 (for 0) + 10 (for 1 thru 10) = 12.
i must be <= 10, so it is 11 to exit the loop and inside the last iteration of the loop, i = 10 = j. k is 1 after the first iteration, while i is -1. Running through the loop, you'll see:
k = 1, i = -1
k = 2, i = 0
k = 3, i = 1
k = 4, i = 2
k = 5, i = 3
k = 6, i = 4
k = 7, i = 5
k = 8, i = 6
k = 9, i = 7
k = 10, i = 8
k = 11, i = 9
k = 12, i = 10
Therefore k = 12
Here are the steps:
When the loop begins, all three variables are zero.
The loop initializer sets i to minus 1.
Loop test: i <= 10 is true, so loop is entered.
Inside the loop, j is set to i, so j is also minus 1.
k is incremented, so k becomes 1.
the iteration ends; i is incremented because of the ++i, so i becomes 0.
Loop test: since i is zero, i <= 10 is true, so the loop is entered again.
In this way, the loop continues, changing j, k, and i in that order. So when i becomes 10, j will be 9 and k 11. At that point:
The loop is entered for the last time.
j becomes 10 as well; k becomes 12
Then i gets incremented to 11. The loop condition i <= 10 is false, and the loop terminates.
So i is 11. j is 10, k is 12 when the loop terminates.
The key point is, after the first pass, every time the loop is entered, j is one less than i, and k is one greater than i. When the loop terminates, this is still the case.
for (i=-1; i<=10; ++i){
j = i; ++k;
}
Here is the loop :
i = i +1; <-------+
| |
check condition!------|--+
| | |
j = i; | |
| | |
k++;----------------+ |
| |
+<--------------------+
|
other code
at last loop
i = 10
condition == true
j = 10;
k = 12;
Then
i= i+1 means i = 11 but the condition show false! loop end.
Take three variables separate.
You can see the variable k would be incremented , the number of times the loop is executed.
The no. of time sit would be executed from -1 to 10 it would have done 12 iterations
k = 1, i = -1, j=-1
k = 2, i = 0, j=0
k = 3, i = 1, j=1
k = 4, i = 2, j=2
k = 5, i = 3, j=3
k = 6, i = 4, j=4
k = 7, i = 5, j=5
k = 8, i = 6, j=6
k = 9, i = 7, j=7
k = 10, i = 8, j=8
k = 11, i = 9, j=9
k = 12, i = 10, j=10
After This i has reached its limit, but it will first increment and then check,
hence i=11, k=12 and j to a one less than the value of i i.e j= 10