#include <stdio.h>
int i = 3, j = 10;
int crypt(int j)
{
return (i = i+j);
}
void decrypt(int x, int i)
{
j += crypt(i);
}
int main(void)
{
int i = 0;
i = crypt(5);
decrypt(i, j);
printf("|%d %d|", i, j);
return 0;
}
I'm having trouble figuring out why does it printout |8 28|.
The "8" part, I understand that at
i = crypt(5) -> j is now 5 in this function -> i = i + j -> There's no i therefore it uses the global variable i = 3 -> i = 3 + 5 -> returns i = 8
So the i in the main function becomes 8.
But what about the next printout? Why is it 28 instead of 23?
The way I read it was like this
decrypt(i, j) -> decrypt(8, 10) -> x is now 8 and i is now 10 in this function -> j += crypt(i) -> j += crypt(10) -> j in this function is now 10.
return ( i = i + j ), there's no i in this function so i = 3 + 10... returns 13?
So then j += 13 is 23?
Which part of the step did I mess up? I've been reading local / global scope online and I still don't quite get where did I go wrong... Feels like I'm messing up my value for i somewhere.
PS: I apologize for the poor formatting, not quite sure how else can I put it cleanly.
You write:
return ( i = i + j ), there's no i in this function so i = 3 + 10... returns 13?
No, i
is not 3 anymore. It was changed to 8 previously, i.e. here return (i = i+j);
due to the first call of crypt
When you write:
So the i in the main function becomes 8.
it's correct but the global i
was changed as well.