What are C Functions Interview Questions- C Programming Course With Examples

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Free C Programming Course · Chapter 6 · Practice Lab

C Functions Interview Questions

A complete practice lab for function calls, prototypes and the return statement: quiz questions, output-prediction puzzles, a coding challenge and interview questions with answers. Free embedded C programming practice.

10
Quiz questions
3
Output puzzles
12
Interview questions

Reading is only half of learning, so this lecture of our free C programming course turns the last three lessons into practice. If you want a free C tutorial that actually prepares you for interviews, work through the C functions interview questions below before opening the answers. Everything here focuses on embedded C programming: function calls, prototypes and return values as they appear in firmware jobs and screening tests.

What You Will Learn

In this practice lecture

  • Test your understanding of function calls, prototypes and return with a ten-question quiz.
  • Predict program output the way interviewers expect you to.
  • Write and run a small checksum function with a self-test.
  • Answer the C functions interview questions that come up in embedded roles.
  • Review the most common mistakes in one table.

Prerequisites

Complete the three previous lectures on the C function call, the function prototype in C, and the return statement in C. Try each question on paper first. Click an answer only after you have committed to your own.

Quick Concept Map Before You Start

Chapter 6 in one picture
Prototype→compiler learns name, parameters, return type
Call→arguments copied, control jumps to the body
Return→control and value go back to the caller
Result→used in an expression, or discarded as a statement

Quiz: C Functions Interview Questions on Calls and Return

Choose one option for each question, then open the answer.

Q1. Which statement about a function that is defined but never called is true?

  1. It runs once at program start.
  2. Its code is never executed.
  3. It runs when main() ends.
  4. It causes a compile error.
Show answer

B. The body runs only when some other function calls it.

Q2. Given void log_event(int code);, which line is invalid?

  1. log_event(4);
  2. int r = log_event(4);
  3. log_event(2 + 3);
  4. (void)log_event(1);
Show answer

B. A void function produces no value, so it cannot be assigned. gcc reports “void value not ignored as it ought to be”.

Q3. Why is read_adc() = 5; an error?

  1. The name is too short.
  2. A call result is a temporary value, not a storage location.
  3. Assignments need parentheses.
  4. read_adc must be static.
Show answer

B. The left side of an assignment must be an lvalue.

Q4. What three facts does a prototype give the compiler?

  1. Name, parameter types, return type
  2. Name, body, file name
  3. Return type, stack size, address
  4. Parameter names, comments, linkage
Show answer

A.

Q5. Are parameter names required in a prototype?

  1. Yes, always.
  2. Only for int parameters.
  3. No, only the types matter to the compiler.
  4. Only in header files.
Show answer

C. Names are optional documentation.

Q6. In C before C23, what does the declaration void init(); mean?

  1. The function takes exactly zero arguments, checked strictly.
  2. The parameters are unspecified, so calls are not checked.
  3. The function returns an int.
  4. It is a syntax error.
Show answer

B. Use void init(void); to forbid arguments.

Q7. What does return; do inside a void function?

  1. Restarts the function.
  2. Returns zero.
  3. Leaves the function immediately.
  4. Ends the whole program.
Show answer

C.

Q8. A function has three return statements. How many can run in one call?

  1. Exactly one, the first reached
  2. All three
  3. Two
  4. None until the last line
Show answer

A. Execution leaves at the first return reached.

Q9. A non-void function reaches its closing brace without a return, and the caller uses the result. What is the outcome?

  1. It returns 0 safely.
  2. Undefined behaviour
  3. A linker error
  4. The program halts with a message
Show answer

B. The compiler warns with -Wreturn-type.

Q10. Which line makes a function return a dangling pointer?

  1. return &buffer[0]; where buffer is a parameter array from the caller
  2. return &local_value; where local_value is a local variable
  3. return NULL;
  4. return &global_state;
Show answer

B. Local variables vanish when the function returns.

Output Puzzles for C Functions Interview Questions

Write down what each program prints, as interviewers ask in C functions interview questions. All were compiled with gcc and run, and the outputs shown are real.

Puzzle 1: the returned increment

#include <stdio.h>

int bump(int v) { return v++; }

int main(void)
{
    int a = 5;
    printf("%d %d\n", bump(a), a);
    return 0;
}
Show output and reason

Output: 5 5. The function returns the old value of its local copy, and the increment affects only that copy, so the caller’s variable stays at 5.

Puzzle 2: which statements run?

#include <stdio.h>

int f(int n)
{
    if (n > 2)
        return 10;
    printf("A");
    return 20;
    printf("B");
}

int main(void)
{
    printf("%d\n", f(1) + f(5));
    return 0;
}
Show output and reason

Output: A30. The call f(1) prints A and returns 20. The call f(5) returns 10 at the first return. The line that prints B sits after a return, so it never runs. Only one of the two calls prints, so the order in which they are evaluated does not change the output.

Puzzle 3: truncated result

#include <stdio.h>

static int half(float x) { return x / 2; }

int main(void)
{
    printf("%d\n", half(7.0f));
    return 0;
}
Show output and reason

Output: 3. The value 3.5 is converted to the declared return type int, which drops the fraction.

Coding Challenge: Checksum with a Self-Test

Write a function int checksum8(const unsigned char *buf, int len) that returns the sum of all bytes modulo 256, or -1 if the buffer is NULL or the length is not positive. Use a prototype above main, an early return for the bad-input case, and a helper that prints PASS or FAIL. Try it yourself first, then compare with this solution saved as ep_fn_lab.c.

#include <stdio.h>

static int checksum8(const unsigned char *buf, int len);
static void report(const char *tag, int ok);

static int checksum8(const unsigned char *buf, int len)
{
    int sum = 0;
    if (buf == NULL || len <= 0)
        return -1;
    for (int i = 0; i < len; i++)
        sum = (sum + buf[i]) & 0xFF;
    return sum;
}

static void report(const char *tag, int ok)
{
    printf("%-10s : %s\n", tag, ok ? "PASS" : "FAIL");
}

int main(void)
{
    unsigned char frame[] = { 0x10, 0x20, 0x30, 0xF0 };
    int cs = checksum8(frame, 4);
    printf("checksum = 0x%02X\n", cs);
    report("checksum", cs == 0x50);
    report("null-buf", checksum8(NULL, 4) == -1);
    return 0;
}
$ gcc -Wall -Wextra -o ep_fn_lab ep_fn_lab.c
$ ./ep_fn_lab
checksum = 0x50
checksum   : PASS
null-buf   : PASS

The four bytes add up to 0x150, and masking with 0xFF keeps the low byte, 0x50. Notice how the function uses every idea from this chapter: prototypes first, an early return for bad input, a value return for the result, a void function called as a statement, and a comparison used as an argument.

Stretch tasks

  • Change the function so it returns the two’s complement checksum, so that adding it to the sum gives zero modulo 256.
  • Add a third test with a length of zero.
  • Move the prototypes into a header and split the program into two source files.

C Functions Interview Questions with Answers

These are the C functions interview questions most often asked in embedded and firmware screening rounds. Say your answer aloud before opening it.

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

A declaration tells the compiler the name, parameter types and return type and ends with a semicolon. A definition also contains the body and produces code. A program may declare a function many times but define it once.

2. Why do we need function prototypes?

So the compiler can check every call against the right number and types of arguments, convert arguments correctly, and know the return type before it sees the definition.

3. What is the difference between f() and f(void)?

Before C23, f() leaves the parameter list unspecified and disables checking of calls; f(void) states that there are no parameters and the compiler enforces it.

4. Can a function call appear on the left side of an assignment?

No, because the result is a temporary value and not an lvalue. A function returning a pointer can be dereferenced to assign through it, but the call itself is not assignable.

5. What happens if you ignore the return value of a function?

It is discarded. That is legal, but errors may go unnoticed. Cast to void to show intent when you deliberately ignore a result.

6. Can a void function return a value?

No. A void function may use return; with no value, but it cannot supply one to the caller.

7. How are arguments passed in C?

By value. The callee receives copies, so changing a parameter does not change the caller’s variable. To modify the caller’s data, pass a pointer.

8. Is the order of evaluation of function arguments defined?

No, it is unspecified. Avoid arguments that depend on each other’s side effects, such as f(i++, i).

9. What is wrong with returning a pointer to a local variable?

The local object ceases to exist when the function returns, so the pointer dangles. Use caller-supplied storage, a static object, or return the value itself.

10. What happens if a non-void function has no return on some path?

If the caller uses the returned value, the behaviour is undefined. Compile with -Wall to get the warning.

11. How does the compiler treat a call to an undeclared function in modern C?

Implicit declarations were removed in C99. Compilers warn, and recent releases such as gcc 14 report an error by default.

12. Why would an embedded team prefer a single return per function?

Some coding standards, for example MISRA C:2012 in an advisory rule, favour a single exit for traceability. Other teams prefer early returns to reduce nesting. The right answer is to follow the project standard and justify the exceptions.

Common Mistakes Recap

MistakeSymptomFix
Using a void call as a valueCompile errorGive the function a real return type
Call on the left of =Compile error, lvalue requiredAssign to a variable instead
No prototypeImplicit declaration warning or errorDeclare it or include its header
Prototype and definition differSilent run-time corruption across filesInclude the shared header in the defining file
Missing returnGarbage valueReturn on every path, enable -Wreturn-type
Returning a local addressDangling pointerUse caller-owned or static storage
Ignoring error codesFailures go unnoticedCheck results or cast to void deliberately

Best Practices, Performance and Security

Best practices

  • Compile every exercise with -Wall -Wextra and read each warning.
  • Prototype every function and keep prototypes in headers.
  • Write a tiny self-test like the PASS and FAIL helper above for each function you create.

Performance considerations

Interviewers sometimes ask about call overhead. The honest answer: a few instructions and some stack space per call, usually negligible, but worth watching in interrupt handlers and tight loops. Small static functions can be inlined by the compiler.

Security considerations

Validate inputs at the top of a function with early returns, as the checksum example does for a NULL buffer. Always check return codes of functions that can fail, particularly in code that handles external data.

Summary and Key Takeaways

Chapter 6 so far

  • A call is an expression; with a semicolon it is a statement.
  • A prototype states the name, parameter types and return type.
  • Return ends the function; the value is converted to the declared type.
  • Most function bugs show up as compiler warnings if you enable them.

Conclusion

You have now practised the full cycle of a function in C: declare it, call it, and return from it. Working through quiz questions, predicting output and answering interview questions is what turns knowledge into reflex, and that reflex is exactly what hiring managers look for in embedded candidates. Keep this page bookmarked and revisit the output puzzles after a few days. When you are ready, continue with the rest of the free embedded C course, where functions meet storage classes, recursion and pointers.

Frequently Asked Questions

Are these C functions interview questions suitable for freshers?

Yes. They start from basics and move up to details such as unspecified evaluation order.

Do I need hardware to practise?

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

Which compiler flags should I use?

Use -Wall -Wextra at minimum, and add -Wstrict-prototypes when you want to catch weak declarations.

Why does the output puzzle with the increment print 5 5?

The function returns the old value of its own copy, and the increment never touches the caller’s variable.

Can I ignore warnings if the program runs?

You should not. Many warnings point at undefined behaviour that appears only on certain boards or optimisation levels.

What should I study after functions?

Storage classes, scope, recursion and pointers, in that order.

Is this a free C programming course?

Yes. EmbeddedPathashala offers this free C programming course for students aiming at embedded systems careers.

How should I answer if an interviewer asks for the difference between declaration and definition?

Say that a declaration describes the function and can repeat, while a definition provides the body and exists once.

Keep Going With the Free Embedded C Course

You finished the practice lab for functions. Continue to the next chapter, share this free C tutorial with a friend, and keep building the embedded C programming skills employers ask for.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

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