C program below
void test_func(int a, int b, int *c) {
a ^= 5;
*c = a + b;
b <<= 4;
}
int main() {
int a = 3, b = 0x011, c = 0;
test_func(a, b, &c);
a = (b > c) ? b : c;
printf ("%d %d %d",a,b,c);
}
The compiler outputs a = 23,b=17 and c = 23 which is not what i expected. I brainstormed step by step in this way :
Main function
Function call is made where a = 3 , b = 17(in int), c=0
and values are passed to the test_func
Functions runs:
a = 3 ^ 5 = 0110 or 6 ;
c = 17+ 6 = 10100 or 23;
b = 17x2^4 = 272 ; (by the formula of bitwise left shift operator)
Return to the main function
b is definitely greater than c so a returns 272 ;
Therefore I expected the result 272,272 and 23 . However the compiler shows 23,17,23. It took me quite a while to realize that b<<=4 never changed the value of b in main function. Please explain why is that so , why didn't the value of b change in main function if values of a and c did?