What is C Function Programs Quiz Lab- C Programming Tutorials For Beginners

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

C Function Programs Quiz Lab

Lecture 27 of our free C programming course: C programming interview questions, a 12-question quiz, output puzzles and coding challenges for embedded C programming.

Welcome to the practice lab of our free C programming course. This page collects C programming interview questions and quiz material for everything in this part of the functions chapter: library functions, old style function syntax, the palindrome, prime and twin prime programs, and decimal to binary conversion. If you are preparing for an embedded C programming interview, this is the place to test yourself.

Work through each section with a notebook and answer before you open the hidden answer. Every output puzzle here was compiled and run, so the answers are real. This free C tutorial page is plain HTML: click a question to reveal its answer, and click again to hide it.

What You Will Learn

  • A 12-question quiz covering library functions, old style syntax and number programs
  • Six output-prediction puzzles with verified answers
  • Five coding challenges with hints and expected results
  • Fifteen C programming interview questions with model answers
  • How to explain compiler errors versus linker errors in an interview
  • How to talk about bit manipulation confidently in an embedded interview

Prerequisites

Before you start

  • You have read the lectures on library functions, K&R style functions, palindromes, primes, twin primes and decimal to binary conversion.
  • You have a C compiler available so that you can try every puzzle yourself.

How to Use This Practice Lab

Do not read the answers first. Try to commit to an answer, then reveal it and compare. When your answer is different, find out why by compiling the code yourself. The best C programming interview questions test understanding, not memory, so the explanations matter more than the final words of each answer.

SectionCountSkill tested
Quiz12 questionsConcepts and vocabulary
Output puzzles6 programsReading code precisely
Coding challenges5 tasksWriting functions from scratch
Interview questions15 questionsExplaining ideas out loud
Practice lab at a glance
12
Quiz questions
6
Output puzzles
5
Coding challenges
15
Interview questions

Quiz: 12 Questions on Functions and Number Programs

Click a question to reveal the answer and the reason.

Q1. Which statement about library functions in C is correct?

They are precompiled, shipped with the compiler, and are not part of the language itself. You normally get object code, not source code.

Q2. What provides the declaration of a library function such as strlen?

A header file, here <string.h>. The definition comes from the library file at link time.

Q3. A program prints undefined reference to 'sqrt'. Which stage reported it?

The linker. The compiler accepted the call because the header declared sqrt. The fix is to link the math library with -lm.

Q4. On a bare-metal microcontroller, what calls main()?

The startup code run by the reset handler. There is no operating system to call it.

Q5. In C11, what does the declaration int f(); mean?

The function returns int and its arguments are unspecified. In C23 it means the same as int f(void);.

Q6. In a K&R style definition, where are the parameter types written?

On separate lines after the closing parenthesis and before the opening brace. The parentheses hold only the names.

Q7. What does the digit-reversal loop return for the input 120?

21. The trailing zero is lost, so a number ending in zero is never a palindrome.

Q8. Why is it enough to test divisors up to the square root of n when checking for a prime?

If n = a * b, at least one factor is no larger than the square root. Larger divisors always pair with a smaller one that was already tested.

Q9. Is 1 a prime number?

No. A prime has exactly two divisors and 1 has only one. A flag-based test that starts at 1 and loops from 2 wrongly reports it as prime unless you reject it first.

Q10. What is the difference between the two numbers of a twin prime pair?

Exactly 2. For example (11, 13) or (17, 19).

Q11. What is 29 in binary?

11101, because 16 + 8 + 4 + 1 = 29.

Q12. Which expression tests whether bit n of reg is set?

(reg >> n) & 1u. It shifts the bit into position 0 and masks everything else away.

Output Puzzles: Predict Before You Run

For each program write down the exact output. All answers below were confirmed by compiling with gcc -Wall -Wextra.

Puzzle 1: digit reversal

#include <stdio.h>
int main(void)
{
    unsigned n = 120, rev = 0;
    while (n) { rev = rev * 10 + n % 10; n /= 10; }
    printf("%u\n", rev);
    return 0;
}
Show the output of puzzle 1

Output: 21
The digits are peeled off as 0, 2, 1, so the reverse is built as 0, 2, then 21. The leading zero of the reversed number does not exist.

Puzzle 2: the flag bug

#include <stdio.h>
static int f(int n)
{
    int i, flag = 1;
    for (i = 2; i * i <= n; i++)
        if (n % i == 0) { flag = 0; break; }
    return flag;
}
int main(void)
{
    printf("%d %d %d %d\n", f(1), f(2), f(9), f(0));
    return 0;
}
Show the output of puzzle 2

Output: 1 1 0 1
For 0, 1 and 2 the loop never runs, so the flag keeps its starting value of 1. The function wrongly calls 0 and 1 prime. Only 9 produces 0, because 3 divides it.

Puzzle 3: digit-building conversion

#include <stdio.h>
static unsigned long conv(unsigned long n)
{
    unsigned long bin = 0, place = 1;
    while (n) { bin += (n % 2) * place; n /= 2; place *= 10; }
    return bin;
}
int main(void)
{
    printf("%lu %lu %lu\n", conv(10), conv(6), conv(1));
    return 0;
}
Show the output of puzzle 3

Output: 1010 110 1
10 is 8 + 2 so its bits are 1010. 6 is 4 + 2 so its bits are 110. 1 is just 1.

Puzzle 4: counting twin pairs

#include <stdio.h>
static int is_prime(unsigned n)
{
    if (n < 2) return 0;
    for (unsigned d = 2; d * d <= n; d++)
        if (n % d == 0) return 0;
    return 1;
}
int main(void)
{
    int pairs = 0;
    for (unsigned p = 3; p + 2 < 20; p += 2)
        if (is_prime(p) && is_prime(p + 2)) pairs++;
    printf("%d\n", pairs);
    return 0;
}
Show the output of puzzle 4

Output: 4
The pairs are (3, 5), (5, 7), (11, 13) and (17, 19). The pair (19, 21) is not tested because 21 is not below 20.

Puzzle 5: reading bits of 29

#include <stdio.h>
int main(void)
{
    unsigned v = 29;
    printf("%u %u %u\n", (v >> 2) & 1u, (v >> 1) & 1u, v >> 3);
    return 0;
}
Show the output of puzzle 5

Output: 1 0 3
29 is 11101. Bit 2 is 1, bit 1 is 0 and shifting right by 3 leaves 11 in binary, which is 3.

Puzzle 6: digit sum with a helper

#include <stdio.h>
static int calc(int x)
{
    return x % 10;
}
int main(void)
{
    int n = 3113, s = 0;
    while (n > 0) { s += calc(n); n /= 10; }
    printf("%d\n", s);
    return 0;
}
Show the output of puzzle 6

Output: 8
The helper returns the last digit each time, so the loop adds 3, 1, 1 and 3, which is 8. Pass by value means main() controls how n shrinks.

Bonus puzzle: two compilers, two answers

Consider the old style declaration int ep_add(); followed by a call ep_add(5) and a definition int ep_add(int a, int b). What happens in C11 and in C23?

Show the answer to the bonus puzzle

In C11 mode with -Wall -Wextra it compiles with no warning and prints an unpredictable garbage value, because the second argument is never supplied. In C23 mode (-std=c2x on GCC 13) the empty parentheses mean (void) and the compiler reports too many arguments to function 'ep_add'.

Coding Challenges

Write each function yourself, compile it with warnings enabled and compare your answer with the expected results. Hints are provided, but the code is yours to write.

Challenge 1: count the digits

Write unsigned ep_count_digits(unsigned long n) that returns how many decimal digits n has. Treat 0 as having 1 digit.

Show the hint and the expected results

Divide by 10 in a loop and count the passes. Handle 0 before the loop. Expected: 3113 gives 4, 7 gives 1, 0 gives 1.

Challenge 2: count primes

Write unsigned ep_count_primes(unsigned limit) that returns how many primes are less than or equal to limit.

Show the hint and the expected results

Reuse a prime test function and a loop from 2. Expected: limit 100 gives 25, and limit 499 gives 95.

Challenge 3: cousin primes

Cousin primes differ by 4. Print all cousin prime pairs below 500 and count them.

Show the hint and the expected results

Copy the twin prime loop and change the gap to 4, keeping the boundary check on the second member. Expected count: 26.

Challenge 4: population count

Write int ep_popcount8(uint8_t v) that returns the number of 1 bits in an 8-bit value.

Show the hint and the expected results

Test the lowest bit with v & 1u, then shift right until v is 0. Expected: 0xA5 gives 4, 0xFF gives 8, 0 gives 0.

Challenge 5: binary palindromes

Write int ep_is_bit_palindrome8(uint8_t v) that returns 1 if the 8 bits of v read the same from both ends.

Show the hint and the expected results

Build a reversed copy bit by bit, like the digit reversal but with shifts, then compare. Expected: 0xA5 gives 1, 0x18 gives 1, 0x1D gives 0.

C Programming Interview Questions and Answers

These C programming interview questions are the ones that come up again and again in embedded rounds. Read each question, answer aloud in your own words, then compare.

1. What is the difference between a library function and a user-defined function?

A library function is precompiled and supplied with the compiler, you get its declaration from a header and its definition from a library file. A user-defined function is written by you, so you provide both the declaration or prototype and the definition.

2. What is the difference between a declaration and a definition of a function?

A declaration tells the compiler the name, argument types and return type, and it may appear many times. A definition provides the body and exists exactly once in the program.

3. What is the difference between an undefined reference error and an implicit declaration warning?

An implicit declaration comes from the compiler when it meets a call with no visible declaration. An undefined reference comes from the linker when a declaration exists but no definition was linked in.

4. Why do you sometimes need the -lm flag?

On many Linux toolchains the math functions live in a separate library, so the linker must be told to search it. Without the flag the call to a function like sqrt() fails to link.

5. Can you define your own function with the same name as a library function?

It is unsafe. The name is reserved, and a compiler may treat it as a built-in and replace the call, so behaviour changes with flags and optimisation. Use a prefix or static.

6. Who calls main()?

Startup code does. On a hosted system the operating system loads the program and the runtime calls main(). On bare metal the reset handler initialises memory and then calls it.

7. What is the difference between int f(); and int f(void);?

In C11 and earlier, empty parentheses mean the arguments are unspecified and calls are not checked. (void) means no arguments and is checked. In C23 both mean no arguments.

8. What is wrong with the K&R function definition style?

The compiler cannot check argument count or types, so wrong calls compile silently. It also splits names and types over several lines and has been removed from the C23 standard.

9. How do you check whether a number is prime efficiently?

Reject numbers below 2, handle 2 and even numbers, then test odd divisors while the square of the divisor does not exceed the number. Return as soon as one divisor is found.

10. Why does a flag-based prime test report 1 as prime?

The flag starts as true and is cleared only inside the loop. For 1 the loop never runs, so the initial value survives. Reject numbers below 2 before the loop.

11. How do you reverse a number in C and what can go wrong?

Use rev = rev * 10 + n % 10 and n /= 10 in a loop. The result can overflow for large inputs, and trailing zeros are lost, so wider types or a half-reverse check are safer.

12. Why does the original variable survive a call to a function that modifies its parameter?

C passes arguments by value. The parameter is a private copy, so changes inside the function do not affect the caller.

13. How do you convert a number to binary in C?

Either divide repeatedly by 2 and read the remainders from last to first, or, preferably in firmware, loop over the bits from the highest to the lowest and print (value >> bit) & 1u.

14. How do you set, clear, toggle and test a bit?

Set with reg |= (1u << n), clear with reg &= ~(1u << n), toggle with reg ^= (1u << n), test with (reg >> n) & 1u.

15. Why do embedded developers often mark helper functions static?

A static function is private to its file, which avoids name clashes with library functions and other files, and it lets the compiler inline or remove unused code to save flash.

C Programming Interview Questions: Common Mistakes

Mistake in the interviewBetter answer
Saying that printf is part of the C languageSay it is a library function declared in <stdio.h>
Calling every build failure a compile errorDistinguish compiler errors from linker errors
Forgetting that 1 is not primeAlways state the base cases first
Writing 1 << 31 with a signed 1Use 1u to avoid signed shift trouble
Claiming empty parentheses mean no argumentsExplain the C11 and C23 difference

Preparing for C Programming Interview Questions

Preparation checklist

  • Compile and run every puzzle yourself, with -Wall -Wextra.
  • Practise explaining each concept in two sentences without jargon.
  • Keep a one-page list of bit manipulation expressions and review it daily.
  • Prepare one example of a bug you found because a compiler warning was switched on.

Summary and Key Takeaways

Remember

  • Library functions are declared by headers and defined by libraries that the linker adds.
  • Compiler errors and linker errors have different causes and different fixes.
  • Prototypes protect you, while old style declarations do not.
  • Test the small inputs, 0 and 1, in every number program.
  • Shift and mask is the standard way to inspect and change bits.

Conclusion

You have now practised the most common C programming interview questions on functions and number programs, predicted output the way an interviewer expects, and written your own helper functions. Keep this page bookmarked and revisit the puzzles after a few days. When you are ready, continue with the next chapter of this free embedded C course, where functions meet storage classes, recursion and pointers.

Frequently Asked Questions

Are these C programming interview questions suitable for freshers?

Yes. They start with vocabulary and basic programs and finish with bit manipulation and C23 differences that impress experienced interviewers.

Do I need a microcontroller to practise?

No. Every program here runs on a normal computer with gcc.

Which compiler flags should I use while practising?

Use -Wall -Wextra as a minimum. Add -Wstrict-prototypes and -Wold-style-definition to see old style code.

Why is it important to know the difference between compiler and linker errors?

It tells you where to look. Compiler errors point at your code or missing headers, linker errors point at missing definitions or libraries.

How many primes are there below 100?

There are 25 primes below 100.

How many twin prime pairs are below 500?

There are 24 pairs and the largest is (461, 463).

Where can I find more embedded C interview preparation?

Continue with the free C programming course on EmbeddedPathashala, where every chapter ends with a quiz and interview questions.

Finished the Practice Lab?

Move on to the next chapter of the free embedded C course, where functions meet storage classes, recursion and pointers.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →