Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 8 of 8

Practice & Quiz - Functions, Scope & Recursion

Test your understanding of prototypes, call-by-value, scope, lifetime, recursion, inline functions, and multi-file programs.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise

Practical Exercise
/* bits.c - bit manipulation showing inline and recursion */
#include <stdio.h>
#include <stdint.h>

/* Power of 2 check - inline bit trick */
static inline int is_power_of_2(unsigned n) {
    return n > 0 && (n & (n - 1)) == 0;
}

/* Recursive popcount */
static int popcount_rec(unsigned n) {
    if (n == 0) return 0;
    return (int)(n & 1) + popcount_rec(n >> 1);
}

/* Reverse bits in a byte */
static uint8_t reverse_bits(uint8_t b) {
    uint8_t r = 0;
    for (int i = 0; i < 8; i++) {
        r = (uint8_t)((r << 1) | (b & 1));
        b >>= 1;
    }
    return r;
}

int main(void) {
    unsigned vals[] = {0,1,2,3,4,8,16,255,256};
    int n = (int)(sizeof(vals)/sizeof(vals[0]));
    for (int i = 0; i < n; i++) {
        printf("%4u  pow2=%-3s  bits=%d
",
               vals[i],
               is_power_of_2(vals[i]) ? "YES" : "no",
               popcount_rec(vals[i]));
    }
    printf("reverse_bits(0xB0) = 0x%02X
", reverse_bits(0xB0));
    return 0;
}
/* gcc -Wall -std=c99 bits.c -o bits && ./bits */

Module Quiz

Module Quiz
Question 1 of 5

Why must a function prototype appear before its call site in C?

Tip

Tip

Practice Practice Quiz Functions Scope Recursion in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Process virtual address space organizes code, static variables, heap, and call stack

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Functions Scope Recursion from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Functions Scope Recursion is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Topics in This Module

0/8 done