This document contains 3 programming exercises from a lecture on C programming. The exercises ask the reader to determine the output of 3 code snippets: [1] A function that calls another function and returns a sum; [2] A function that squares and adds values that is called on two variables; [3] A function that checks if two values have different signs and conditionally prints one of the values.
2. Exercise 1
• What is the output of the following program.
int f1(int a, int b){
a = 2 * m2(a);
return a + b;
}
int f2(int a){
return a % 2;
}
int main(){
int a = 10, b = 20;
printf(“%d”, f1(a, 2));
b = f2(b / a);
printf(“a = %d b = %d“, a, b);
return 0;
}
2
3. Exercise 2
• What is the output of the following program.
int main(){
int a = 10, b = 20;
a = f1(a, b);
b = f1(b, a);
printf(“a = %d b = %d“, a, b);
return 0;
}
int f1(int a, int b){
return a * a + 2 * b;
}
3
4. Exercise 3
• What is the output of the following program.
int main(){
int a, b;
printf(“Enter a b”);
scanf(%d %d”, &a, &b);
if(xor(a, b))
printf(“a = %d “, a);
else
printf(“b = %d “, b);
return 0;
}
int xor(int a, int b){
if(a < 0 && b < 0 || a > 0 && b > 0)
return 0;
else
return 1;
}
4