C programming · practice set

Practice Tasks – Review before the test

A compact revision page based on Laboratories 1–7 and on sample test questions. The tasks focus on test-style questions: input/output, identifiers, control flow, arrays, pointers, structures, header files and separate compilation. They are suitable for solving on paper.

scanfprintfidentifiers loopsarrayspointers structheadersoutput tracing

How to use this page

Try to solve each task first. Then open the answer. Some questions are intentionally close to test format, while others connect directly to the laboratory workflow.

Symbols used in input examples: means ENTER, means SPACE, and means TAB.

1 Reading integers with scanf

Topic: scanf("%d %d", &n, &p), whitespace skipping.

What values are read into n and p?

scanf("%d %d", &n, &p);
CaseUser inputnp
A123⌂45⇒4500⌐??
B123⌐⌐⌂456⇒⇒⇒⇒⌂789⌐??
C123⌂456⌂789⌐??
D⌂000⌂⌂⌂123⇒⇒⇒456⌐??
E⇒⌐⇒000⇒123⇒456⌐??
Answer
Casenp
A12345
B123456
C123456
D0123
E0123

%d skips leading whitespace. It stops reading when the number ends.

2 Field width in scanf

Topic: %4d, %2d.

What values are read into n and p?

scanf("%4d %2d", &n, &p);
CaseUser inputnp
A⇒123⇒45⇒00⌐??
B⇒12345⌂⌂⌂678⌐??
C⌂⌂⌂123⌂⌂⌂456⌂⌂⌂789⌐??
D1234567⌂89⌐??
E⌂⌂⌂0001234⌂⌂⌂456⌐??
Answer
Casenp
A12345
B12345
C12345
D123456
E123

%4d reads at most four characters for the first integer; %2d reads at most two characters for the second one.

3 Valid identifiers

Topic: C names and reserved keywords.

Which identifiers are valid C identifiers?

Axxx11qwerty
By1234xcs
C#see_you#
Dfloat
Esee_you
Fx11_
Gxxxx.xxxx
H_123x321_
I1_1_2_2
JVoiD
Answer

Valid: A, B, E, F, H, J.

Invalid: C, D, G, I.

float is a keyword, so it cannot be used as an identifier. VoiD is valid because C is case-sensitive and it is not the same token as void.

4 Pointers to array elements

Topic: arrays, pointers, dereferencing.

What is printed by this program?

#include <stdio.h>

int main(void) {
    int t[4] = { 10, 20, 30, 40 };
    int *p[4];

    for (int i = 0; i < 4; i++) {
        p[i] = t + i;
    }

    for (int i = 0; i < 4; i++) {
        printf("%d ", *p[i]);
    }

    printf("%d %d\n", *(p[1] + 1), *p[1] + 1);
    return 0;
}
A10 20 30 40 20 21
B10 20 30 40 30 21
C10 20 30 40 30 30
D20 30 40 50 30 21
Answer and explanation

Correct answer: B.

10 20 30 40 30 21

p[i] = t + i means that p[0] points to t[0], p[1] points to t[1], and so on. Therefore *p[i] prints 10 20 30 40.

p[1] points to t[1], whose value is 20. So *(p[1] + 1) means the next element, t[2], which is 30. But *p[1] + 1 means 20 + 1, which is 21.

5 Loop tracing with continue

Topic: infinite loop, continue, break.

What is the output?

int n = 0;
for (;;) {
    if (n % 2 == 0) {
        printf("%d (2)\n", n);
        n += 3;
        continue;
    }
    if (n % 7 == 0) {
        printf("%d (7)\n", n);
        n += 3;
        continue;
    }
    n++;
    if (n > 20) break;
}
A
0 (2)
4 (2)
7 (7)
10 (2)
14 (2)
18 (2)
21 (7)
24 (2)
B
0 (2)
3 (7)
6 (2)
9 (7)
12 (2)
C
0 (2)
2 (2)
4 (2)
6 (2)
8 (2)
Answer and explanation

Correct answer: A.

The key point is that continue skips the rest of the current loop iteration. When n is even, the program prints it, adds 3, and immediately starts the next iteration.

n at startcondition usedprintedn after action
0n % 2 == 00 (2)3
3none4
4n % 2 == 04 (2)7
7n % 7 == 07 (7)10
10n % 2 == 010 (2)13
13none14
14n % 2 == 014 (2)17
17none18
18n % 2 == 018 (2)21
21n % 7 == 021 (7)24
24n % 2 == 024 (2)27

6 Formatted output — choose the correct printf

Topic: printf, width specifiers, alignment.

The following code has one missing line. Which version prints the value of a right-aligned in a field of width 4?

int a = 123;
/* missing line */

Expected output:

 123
Aprintf("%d\n", a);
Bprintf("%4d\n", a);
Cprintf("%-4d\n", a);
Dprintf("%04d\n", a);
Answer

Correct answer: B. %4d prints an integer in a field of width 4 and pads with spaces on the left. %-4d aligns left, and %04d pads with zeroes.

7 Calculator with switch — missing case

Topic: switch, operators, incomplete code.

Choose the correct missing fragment for multiplication.

switch (op) {
    case '+':
        printf("%g", a + b);
        break;

    /* missing fragment */

    default:
        printf("Unsupported operator");
}
A
case '*':
    printf("%g", a * b);
    break;
B
case '*'
    printf("%g", a * b);
C
if (op == '*')
    printf("%g", a * b);
    break;
D
case 'x':
    printf("%g", a * b);
    break;
Answer

Correct answer: A. In a switch, a case label needs a colon. The operator read from input is usually '*', not 'x'. The break prevents falling through into the next case or default.

8 scanf and whitespace before %c

Topic: reading characters safely after numeric input.

After reading a number, the program should ask for one character answer: Y or N. Which scanf is the best choice?

int value;
char answer;

scanf("%d", &value);
printf("Continue? (Y/N) ");
/* missing scanf */
Ascanf("%c", &answer);
Bscanf(" %c", &answer);
Cscanf("%d", &answer);
Dscanf("%s", &answer);
Answer

Correct answer: B. The space before %c tells scanf to skip whitespace, including the ENTER left after the previous number. Without the space, %c may read the newline character instead of the student’s answer.

9 Code tracing — loop output

Topic: for loop and conditional printing.

What is printed?

for (int i = 1; i <= 6; i++) {
    if (i % 2 == 0) {
        printf("%d ", i);
    }
}
A1 2 3 4 5 6
B2 4 6
C1 3 5
Answer

Correct answer: B. Only numbers divisible by 2 are printed.

10 Code tracing — nested loops

Topic: nested loops and aligned output.

What is printed?

for (int row = 1; row <= 3; row++) {
    for (int col = 1; col <= 3; col++) {
        printf("%2d", row * col);
    }
    printf("\n");
}
A
 1 2 3
 2 4 6
 3 6 9
B
 1 1 1
 2 2 2
 3 3 3
C
 1 2 3
 1 2 3
 1 2 3
Answer

Correct answer: A. Each cell contains row * col.

11 Code tracing — pattern

Topic: nested loops, pattern printing.

What is printed?

for (int row = 1; row <= 4; row++) {
    for (int star = 1; star <= row; star++) {
        printf("*");
    }
    printf("\n");
}
A
****
***
**
*
B
*
**
***
****
C
* * * *
Answer

Correct answer: B. In row 1 the inner loop runs once, in row 2 twice, and so on.

13 Function that remembers calls — fill in static

Topic: local static variable.

Which declaration should be inserted so that counter remembers its value between calls?

int counter(void) {
    /* missing declaration */
    calls++;
    return calls;
}
Aint calls = 0;
Bstatic int calls = 0;
Cconst int calls = 0;
Dextern int calls = 0;
Answer

Correct answer: B. A local static variable is initialized only once and keeps its value between function calls. A normal local variable would be created again on every call.

14A POINT with typedef

Topic: structures, typedef, declaring and initializing variables.

Given this type definition, choose the correct declarations or initializations.

typedef struct {
    float x;
    float y;
} POINT;
APOINT p1 = { 1.0f, 2.0f };
BPOINT p2; p2.x = 1.0f; p2.y = 2.0f;
Cstruct POINT p3 = { 1.0f, 2.0f };
DPOINT.x = 1.0f;
EPOINT p4; p4 = { 1.0f, 2.0f };
Answer

Correct: A, B. Incorrect: C, D, E.

With typedef, POINT becomes a type alias, so variables can be declared as POINT p. struct POINT is not available here, because the structure has no tag name. POINT.x is wrong because POINT is a type, not a variable.

14B struct POINT without typedef

Topic: structures, structure tags, declaring and initializing variables.

Given this structure definition, choose the correct declarations or initializations.

struct POINT {
    float x;
    float y;
};
Astruct POINT p1 = { 1.0f, 2.0f };
Bstruct POINT p2; p2.x = 1.0f; p2.y = 2.0f;
CPOINT p3 = { 1.0f, 2.0f };
Dstruct POINT.x = 1.0f;
Estruct POINT p4 = { .x = 1.0f, .y = 2.0f };
Answer

Correct: A, B, E. Incorrect: C, D.

Without typedef, the type name is struct POINT, not POINT. Dot notation is used on variables such as p2.x, not on the type name itself.

15 Header file and source file

Topic: declarations, definitions, public interface, implementation.

For each element, choose where it should usually be placed in a small C module.

ElementA. Header file .hB. Source file .c
Include guards??
Public typedef needed by other files??
Function declaration / prototype??
Function definition / body??
Private helper function used only in this one file??
Answer
ElementPlace
Include guards.h
Public typedef needed by other files.h
Function declaration / prototype.h
Function definition / body.c
Private helper function used only in this one file.c, usually with static
#ifndef POINT_H
#define POINT_H

/* public type definitions and function declarations */

#endif

16 Separate compilation

Topic: object files and linking.

You have three files: main.c, point.c, point.h. Which command compiles and links the program?

Agcc -Wall main.c point.c -o program
Bgcc -Wall point.h -o program
Cgcc -Wall main.c -o program
Answer

A is correct. The header is included by source files; it is not compiled as a separate translation unit.

17 Math library

Topic: math.h and -lm.

You use sqrt in a program. Which command is normally correct on the AGH UNIX server?

Agcc -Wall program.c -o program
Bgcc -Wall program.c -lm -o program
Cgcc -Wall math.h program.c -o program
Answer

B. #include <math.h> gives declarations. -lm links the math library.

22 Types of expressions

Topic: arrays, pointers, address-of operator.

Write the type of each expression.

DeclarationExpressionType
int x;x?
int x;&x?
int x[2];x?
int x[2];*x?
int x[2];&x?
int x[4][3];x?
int x[4][3];*x?
int x[4][3];**x?
int x[4][3];&x?
Answer
ExpressionType
x where int x;int
&x where int x;int *
x where int x[2];int * in most expressions
*x where int x[2];int
&x where int x[2];int (*)[2]
x where int x[4][3];int (*)[3] in most expressions
*x where int x[4][3];int * in most expressions
**x where int x[4][3];int
&x where int x[4][3];int (*)[4][3]

Checklist before the test

  • I know how scanf treats whitespace and field width.
  • I can trace loops with continue and break.
  • I understand array decay and pointer arithmetic in simple examples.
  • I can split a program into .h and .c files.