1. C Language Code Analysis Practice
- Just like language, in programming code, not only writing ability but also reading ability is important.
- The ability to read code becomes the foundation of analytical skills for finding bugs that occur during programming and predicting code behavior in advance.
- Analyze the code presented below directly without compiling it and predict the execution result.
- Through this, you can build code comprehension skills and practice logical thinking.
1.1. Beginner Level
1.1.1. ex01_low_if.c
- Understand the control flow of the
if/else conditional statement.
- Check operator precedence in the expression
a + b > c * 3.
- Trace how constant values declared with
const are used in condition evaluation.
#include <stdio.h>
int main(void)
{
const int a = 3;
const int b = 5;
const int c = 2;
int result = 0;
if (a + b > c * 3) {
result = 1;
} else {
result = 2;
}
printf("%d\n", result);
return 0;
}
1.1.2. ex02_low_while_sum.c
- Understand the process of accumulating values using a
while loop.
- Check the loop termination condition defined by
#define MAX_NUM.
- Trace how the value of
sum increases in each iteration.
#include <stdio.h>
#define MAX_NUM 5
int main(void)
{
int i = 1;
int sum = 0;
while (i <= MAX_NUM) {
sum = sum + i;
i = i + 1;
}
printf("%d\n", sum);
return 0;
}
1.1.3. ex03_low_switch.c
- Understand the flow of the
switch/case statement and the subsequent if/else statement.
- Trace which
case is selected according to the const-defined value n, and how score changes afterward.
#include <stdio.h>
int main(void) {
const int n = 4;
int score = 0;
switch (n % 3) {
case 0:
score = 10;
break;
case 1:
score = 20;
break;
default:
score = 30;
break;
}
if (score > 15) {
score = score - 5;
} else {
score = score + 5;
}
printf("%d\n", score);
return 0;
}
1.1.4. ex04_low_nested_if.c
- Understand the nested loop structure and the process of counting divisors using it.
- Trace the method of determining prime numbers through the condition
count == 2 and the cumulative changes of sum.
#include <stdio.h>
int main(void)
{
const int x = 7;
const int y = 4;
int out = 0;
if (x > y) {
if ((x - y) % 2 == 1) {
out = 9;
} else {
out = 6;
}
} else {
out = 3;
}
printf("%d\n", out);
return 0;
}
1.2. Intermediate Level
1.2.1. ex05_mid_for_break.c
- Understand the flow in which the
for loop is interrupted by a break statement.
- Check the condition evaluation process using the constants
#define LOOP_MAX and #define THRESHOLD.
#include <stdio.h>
#define LOOP_MAX 10
#define THRESHOLD 20
int main(void)
{
int i;
int answer = 0;
for (i = 1; i <= LOOP_MAX; i = i + 1) {
if (i * i > THRESHOLD) {
answer = i;
break;
}
}
printf("%d\n", answer);
return 0;
}
1.2.2. ex06_mid_function_chain.c
- Understand the value transformation process caused by function calls.
- When calling the
transform() function twice in succession, trace how input values and return values change.
#include <stdio.h>
int transform(int n)
{
int v = n;
if (v % 2 == 0) {
v = v / 2;
} else {
v = v * 3 + 1;
}
return v - 1;
}
int main(void)
{
int x = 6;
x = transform(x);
x = transform(x);
printf("%d\n", x);
return 0;
}
1.2.3. ex07_mid_do_while_reverse.c
- Understand the
do/while loop and the way numeric digits are processed.
- Starting from
init_n, trace the process in which digits are processed from right to left and reversed is updated.
#include <stdio.h>
int main(void)
{
const int init_n = 321;
int n = init_n;
int reversed = 0;
do {
reversed = reversed * 10 + (n % 10);
n = n / 10;
} while (n > 0);
printf("%d\n", reversed);
return 0;
}
1.3. Advanced Level
1.3.1. ex08_high_prime_sum.c
- Understand the nested loop structure and the process of counting divisors using it.
- Trace the condition when
count == 2 (the condition for determining a prime) and the changes in the sum variable.
#include <stdio.h>
int main(void)
{
int n;
int sum = 0;
for (n = 1; n <= 6; n = n + 1) {
int d;
int count = 0;
for (d = 1; d <= n; d = d + 1) {
if (n % d == 0) {
count = count + 1;
}
}
if (count == 2) {
sum = sum + n;
}
}
printf("%d\n", sum);
return 0;
}
1.3.2. ex09_high_recursion.c
- Understand how recursive functions are called.
- Starting from
INITIAL_VAL, trace the call stack flow and even/odd branch behavior when calling calc().
#include <stdio.h>
#define INITIAL_VAL 6
int calc(int n)
{
if (n <= 1) {
return 1;
}
if (n % 2 == 0) {
return n + calc(n - 1);
}
return calc(n - 1);
}
int main(void)
{
printf("%d\n", calc(INITIAL_VAL));
return 0;
}
1.3.3. ex10_high_state_flow.c
- Understand state-based control flow.
- Trace how the value of
n changes according to transitions of state inside the loop.
#include <stdio.h>
int main(void)
{
int n = 23;
int state = 0;
int steps = 0;
while (n > 0) {
if (state == 0) {
n = n - 2;
state = 1;
} else if (state == 1) {
n = n / 2;
state = 2;
} else {
n = n - 1;
state = 0;
}
steps = steps + 1;
}
printf("%d\n", steps);
return 0;
}