#include
@ganeshie8
@dan815 @Compassionate
@shereenkhan :) lol
@magepker728
@magepker728
hello
@shaik0124
yes @micahm
how can i help
explain this statement l = i++ || foo(&j, &k);
if that don't help let me know
i didn't get that u explain
@King.Void. @Conqueror
Don't know any of this stuff, sorry.
"explain this statement l = i++ || foo(&j, &k);" Due to the || you get a true or false result from that and you need to look at the order of operations on || to see what may or may not happen.
Ok we'll try to review this program step by step. First, we'll try to understand what the `foo()` function does. ``` int foo(int* a, int* b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } ``` `foo()` expects to take two pointers `a` and `b` as its parameters. They point to `int` values. That's the meaning of `int* a` for example. In this case `a` is a pointer to an int value and `*a` is the pointed int. `int sum = *a + *b;` computes the sum of the two int (straightforward). `*b = *a;` swaps the values pointed by `a` and `b` `return *a = sum - *b;` does several things: 1. Take a sheet of paper and a pen, give values to `*a` and `*b` and you'll easily realize that this expression always resolves in what was `*b` at the start of the function (before the swap) 2. It puts the result in `*a` 3. It returns `*a` To summarize, at the end of the function `b` points to a value that was the one of `a` and `a` points to a value that was the one of `b`. So `foo()`is a swapping function and could have been named `swaps()`, but it would have been too easy then :-) Now let't talk about the `main()` function. ``` int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&j, &k); printf("%d %d %d %d", i, j, k, l); return 0; } ``` `int i = 0, j = 1, k = 2, l;` declares and initializes some variables. `l = i++ || foo(&j, &k);` is more intricated. Since `i` was initialized to 0, `i++` will increment `i` (which will be equal to 1), but only after the current statement has been evaluated. It's really like writing it this way: ``` l = i || foo(&j, &k); i++; ``` So for the moment we still have i = 0, which is a falsy value in C. So the second part of the OR statement is evaluated, and `foo()` is called. The parameters of the call are the pointers to the `j` and the `k` variables. And we know thats `foo()` swaps the pointed values, right ? So finally, the last (interesting) line ! `printf("%d %d %d %d", i, j, k, l);` `i` is equal to 1 `j` and `k` are swapped, so `j`=2 and `k`=1 And `l` is the result of the OR operator, which is 1 since one of the two values is different from 0. You can confirm by typing this code.
Join our real-time social learning platform and learn together with your friends!