What is the output of this C program that uses a reusable function to calculate the square of a number?
#include <stdio.h> int square(int x) { return x * x; } int main() { int a = 3, b = 4; printf("%d %d\n", square(a), square(b)); return 0; }
Remember the function returns the square of the input.
The function square multiplies the input by itself. So square(3) is 9 and square(4) is 16.
Given the following files, what will be the output when compiled and run?
math_ops.h
int add(int x, int y);
math_ops.c
int add(int x, int y) { return x + y; }
main.c
#include
#include "math_ops.h"
int main() {
printf("%d\n", add(5, 7));
return 0;
}
Check if the function is properly declared and defined.
The function add is declared in the header and defined in math_ops.c. When linked properly, it returns 5 + 7 = 12.
What causes the unexpected output in this C program that tries to reuse a function to increment a value?
#include <stdio.h>
void increment(int x) {
x = x + 1;
}
int main() {
int a = 5;
increment(a);
printf("%d\n", a);
return 0;
}Think about how arguments are passed to functions in C.
In C, function arguments are passed by value, so increment changes only a copy of a. The original a remains unchanged.
Which option contains the syntax error that prevents this modular C code from compiling?
#include <stdio.h>
int multiply(int x, int y) {
return x * y
}
int main() {
printf("%d\n", multiply(3, 4));
return 0;
}Check punctuation carefully in function bodies.
The return statement in multiply lacks a semicolon, causing a syntax error.
Consider a C program split into three files: main.c, utils.c, and math.c. utils.c has 4 functions, math.c has 3 functions, and main.c has 2 functions. How many functions are available for reuse across the entire program after linking?
Count all functions defined in all files.
All 4 + 3 + 2 functions are compiled and linked, so 9 functions are available for reuse.