Skip to content
Justice Tech School

C Exercises

Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.

C Introduction

Exercise

Which function prints text in C?

("Hi\n");

C Syntax

Exercise

Which character ends a statement?

printf("x")

Comments

Exercise

Close the block comment.

/* note 

C Get Started

Exercise

Compile with gcc.

gcc hello.c  hello

How C Compiles

Exercise

Compile only to an object file.

gcc  main.c

Variables and Types

Exercise

Which specifier prints an int?

printf("", 5);

Data Types and sizeof

Exercise

Find the size of a type in bytes.

printf("%zu", (int));

Type Conversion

Exercise

Convert to double before dividing.

() a / b

Operators

Exercise

Compare for equality.

if (a  b)

Booleans

Exercise

Which value counts as false in C?

if () { } /* never runs */

User Input

Exercise

Pass the address of the variable.

scanf("%d", age);

printf and Format Specifiers

Exercise

Print a double.

printf("%", 2.5);

Constants and Macros

Exercise

Define a macro.

# SIZE 10

Integer Types and Limits

Exercise

Fixed size 32 bit signed type.

 x;

Floating Point Numbers

Exercise

Absolute value of a double.

(x)

Characters and ctype

Exercise

Uppercase a char.

(c)

Bitwise Operators

Exercise

Test bit 3.

if (flags  (1u << 3)) {}

Variables and Types

Exercise

Declare an integer.

 n = 5;

sizeof Operator

Exercise

Size in bytes.

(int)

Integer and Float Division

Exercise

Remainder.

7  2

Reading Input with scanf

Exercise

Address of n.

scanf("%d", n);

Strings as char Arrays

Exercise

String end marker.

The string ends with \

string.h Functions

Exercise

Compare strings.

(a, b)

math.h Functions

Exercise

Power.

(2, 3)

Constants and enum

Exercise

Declare an enum.

 Color { RED };

Operators

Exercise

Logical and.

a  b

Type Casting

Exercise

Cast syntax.

() 9.99

printf Formatting

Exercise

Zero pad width 5.

"%d"

If ... Else

Exercise

Run when the condition is false.

if (x) {}  {}

Switch

Exercise

Stop the case from falling through.

case 1: printf("a"); ;

Loops

Exercise

Repeat while the condition holds.

 (n > 0) { n--; }

Ternary, Comma and goto

Exercise

Pick one of two values.

int m = a > b  a : b;

do while, break and continue

Exercise

Skip one round.

if (x) ;

Nested Loops

Exercise

Print a star.

printf("");

if, else if and else

Exercise

Else branch.

if (x) {}  {}

switch and break

Exercise

Stop fall through.

case 1: x(); ;

for, while and do while

Exercise

Run once at least.

 { } while (0);

break and continue

Exercise

Skip a round.

;

Nested Loops and Patterns

Exercise

Inner loop variable.

for (int c = 1; c <= ; c++)

Conditional Operator

Exercise

Ternary symbol.

x > 0  1 : 0

Example: Primes and GCD

Exercise

Greatest common divisor.

gcd(a, b) uses 

Arrays

Exercise

The first index of an array is...

nums[]

Strings

Exercise

Find the length of a string.

strlen()

Functions

Exercise

A function with no result returns...

 greet(void) {}

Recursion

Exercise

Name the part that stops the calls.

if (n <= 1) return 1; /*  case */

Two Dimensional Arrays

Exercise

Declare 2 rows, 3 columns.

int m[2]3];

String Functions

Exercise

Compare strings.

("a", "b")

Converting Strings and Numbers

Exercise

Safe formatted write.

(buf, sizeof buf, "x")

Function Prototypes and Headers

Exercise

End a prototype.

int add(int a, int b)

Scope, static and extern

Exercise

Keep a value between calls.

 int n = 0;

Function Pointers and Callbacks

Exercise

Pointer to int function.

int (*f)int)

Variadic Functions

Exercise

Read the next argument.

va_arg(ap, )

Arrays

Exercise

First index.

a[]

Passing Arrays to Functions

Exercise

Pass the size too.

int total(int *a, int )

Functions

Exercise

Return type for nothing.

 hello() {}

Pass by Value and Pointers

Exercise

Pointer parameter.

void f(int p)

Recursion

Exercise

Base case result.

if (n <= 1) return ;

static Local Variables

Exercise

Keep between calls.

 int n = 0;

Two Dimensional Arrays

Exercise

Second row first column.

g[][0]

ctype Functions

Exercise

Test a digit.

(c)

Converting Strings

Exercise

Safe formatted write.

(buf, size, "%d", n)

Bitwise Operators

Exercise

Shift left.

1  3

Pointers

Exercise

Read the value a pointer points to.

int v = p;

Pointers and Functions

Exercise

Pass the address of x.

swap(x, &y);

Pointer Arithmetic and Arrays

Exercise

Move to the next element.

p;

Dynamic Memory

Exercise

Release the memory.

(arr);

Pointers to Pointers and const

Exercise

Address-of operator.

int *p = x;

Arrays and Pointers Together

Exercise

Pointer arithmetic form of a[2].

*(a + )

Common Memory Bugs

Exercise

Detect memory errors.

gcc -fsanitize= x.c

calloc, realloc and Dynamic Arrays

Exercise

Allocate zeroed memory.

int *v = (4, sizeof *v);

memcpy, memset and memmove

Exercise

Fill with zeros.

(a, 0, sizeof a)

Pointers

Exercise

Address of.

int *p = x;

Pointer Arithmetic

Exercise

Next element.

*(p  1)

malloc and free

Exercise

Release memory.

(a);

calloc and realloc

Exercise

Zeroed memory.

(n, size)

Dynamic Strings

Exercise

Space for the terminator.

strlen(s) + 

NULL and Dangling Pointers

Exercise

Safe empty pointer.

int *p = ;

Pointers to Pointers

Exercise

Array of strings.

char *names[] = {"a"}; char  pp = names;

memcpy and memset

Exercise

Fill bytes.

(p, 0, n)

Structures

Exercise

Access a member through a pointer.

plevel

Enums and Unions

Exercise

Define a set of named constants.

 Level { LOW, HIGH };

File Handling

Exercise

Open a file for reading.

fopen("a.txt", "")

The Preprocessor and math.h

Exercise

Create a constant with the preprocessor.

# PI 3.14

Structs and Pointers

Exercise

Access a field through a pointer.

pscore

Linked Lists

Exercise

End of the list marker.

node->next = ;

Unions and Bit Fields

Exercise

Share memory.

 Num { int i; float f; };

Reading and Writing Text Files

Exercise

Open for appending.

fopen("a.txt", "")

Binary Files

Exercise

Write raw bytes.

(buf, 1, n, f)

Error Handling with errno

Exercise

Message for an error code.

(errno)

Structures

Exercise

Member through pointer.

qage

typedef

Exercise

Name a type.

 int Id;

Arrays of Structures

Exercise

Total quantity.

total += items[i].

Structures and Functions

Exercise

Pointer to struct.

int area(const Rect r)

Unions

Exercise

Shared memory type.

 V {}

Linked List

Exercise

Next pointer.

struct Node *;

Writing and Reading Files

Exercise

Close a file.

(f);

Command Line Arguments

Exercise

Argument count.

int main(int , char **argv)

Error Handling

Exercise

Describe an error.

("open")

Header Files and Modules

Exercise

Guard macro.

#ifndef 

Practice: Word Counter

Exercise

State flag.

in = ;

Practice: Reverse a String

Exercise

Last index.

s[n - ]

Practice: Bubble Sort

Exercise

Compare neighbours.

if (a[j]  a[j + 1])

sizeof, Padding and Alignment

Premium exercise. See plans

Multiple Files and Makefiles

Premium exercise. See plans

Enums and Flag Sets

Premium exercise. See plans

Project: Hash Table

Premium exercise. See plans

Threads with pthreads

Premium exercise. See plans

Processes and Signals

Premium exercise. See plans

Sockets Overview

Premium exercise. See plans

Debugging with gdb and valgrind

Premium exercise. See plans

Testing and assert

Premium exercise. See plans

Writing Portable and Safe C

Premium exercise. See plans

C for Embedded Systems

Premium exercise. See plans

C Best Practices

Premium exercise. See plans

Practice Projects

Exercise

Classic book authors.

K and