🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Functions Quiz and Interview Questions
Lecture 20 of our free C programming course: test yourself on arguments, return values, declarations and main, then practise the interview questions asked for embedded C programming roles.
You have finished the theory of this part of the free C programming course, so it is time to check what stuck. This practice lecture of our free C tutorial has multiple choice questions, output prediction exercises, small coding challenges and a long list of interview questions with model answers. Companies hiring for embedded C programming and C programming for embedded systems roles love these topics, so treat this page as interview preparation from a free embedded C course. Try each question yourself before opening its answer.
What You Will Learn
- Check your grasp of actual and formal arguments and return values
- Practise predicting output of small programs
- Rehearse model answers to common interview questions
- Discover which topics deserve a second reading
Prerequisites
Complete lectures 14 to 19 of this chapter. Keep a compiler open so you can verify your guesses with gcc and the warning flags used throughout the lectures.
How to Use This Page
- Read the question and write your answer on paper
- Open the answer box and compare
- For code questions, compile and run the program
- Note every miss and reread the matching lecture
Scoring guide
Count your correct answers in section A. Ten or more means you are ready for the next chapter. Six to nine means review the lectures you missed. Below six means reread this chapter once and try again tomorrow.
Section A: Multiple Choice Questions
Question 1
In the statement total = add_tax(price, rate); the variables price and rate are called:
- Formal arguments
- Actual arguments
- Return values
- Prototypes
Show answer
Answer: b. They are supplied in the call, so they are actual arguments. The parameters in the function header are the formal arguments.
Question 2
A function void bump(int x) { x = x + 1; } is called as bump(n); with n equal to 5. What is n afterwards?
- 6
- 5
- 0
- Garbage
Show answer
Answer: b. C passes arguments by value, so only the private copy x changed.
Question 3
In classic C (before C23), the declaration void tick(); tells the compiler that:
- The function takes no arguments, and calls are checked
- The function returns an int
- The parameter list is unspecified, so calls are not checked
- The function is defined in another file
Show answer
Answer: c. Empty parentheses leave the parameters unspecified. Write void tick(void) for a checked, parameterless function.
Question 4
Which line causes a compile time error?
void say(void);
int get(void);
int main(void)
{
int a = get(); /* line 1 */
say(); /* line 2 */
int b = say(); /* line 3 */
get(); /* line 4 */
return 0;
}
- Line 1
- Line 2
- Line 3
- Line 4
Show answer
Answer: c. A void function produces no value, so it cannot initialize b. Ignoring the result of get on line 4 is legal.
Question 5
When is a separate prototype not required for a function?
- When it returns float
- When it takes no arguments
- When its full definition appears before every call
- When it is called only once
Show answer
Answer: c. A definition placed earlier acts as the declaration for later calls.
Question 6
The statement result = mix(k, k++); is:
- Always evaluated left to right
- Always evaluated right to left
- Undefined behavior because k is read and modified without ordering
- Rejected by every compiler
Show answer
Answer: c. The order is unspecified and the unsequenced read and modification make the program invalid. Many compilers only warn.
Question 7
A hosted C program ends with return 0; from main. This means:
- The program failed
- The program finished successfully
- The program returned the number zero to every function
- The program should run again
Show answer
Answer: b. Zero is the conventional success status, and nonzero values indicate errors.
Question 8
You run ./logger start fast. What is the value of argc inside main?
- 1
- 2
- 3
- 0
Show answer
Answer: c. The program name counts as the first entry, so there are three.
Question 9
A function declared as void set_level(int v) is called as set_level(7.9); with a prototype visible. What does v hold?
- 8
- 7.9
- 7
- Garbage
Show answer
Answer: c. The double is converted to int and the fraction is truncated, not rounded.
Question 10
On a bare metal microcontroller, main usually:
- Returns an exit status to the operating system
- Never returns and ends with an infinite loop
- Is called by the user through a terminal
- Does not exist
Show answer
Answer: b. No operating system waits for a status, so firmware loops forever inside main.
Question 11
Which statement about exit is correct?
- It can end the whole program from any function
- It works only inside main
- It restarts the program
- It returns control to the previous function
Show answer
Answer: a. It terminates the program from anywhere, after running cleanup handlers and flushing streams.
Question 12
Why was relying on default int for undeclared functions removed from modern C?
- It made programs too fast
- It hid wrong argument and return type bugs
- It increased program size
- It was never part of C
Show answer
Answer: b. Unchecked calls led to hard to find bugs, so C99 required declarations.
Section B: Predict the Output
Program 1
#include <stdio.h>
int sq(int x)
{
return x * x;
}
int main(void)
{
printf("%d\n", sq(3) + sq(4));
return 0;
}
Show answer
Output: 25. Each call returns its own square, 9 and 16, and the sum is printed.
Program 2
#include <stdio.h>
void raise(int level)
{
level = level + 10;
printf("inside: %d\n", level);
}
int main(void)
{
int level = 5;
raise(level);
printf("outside: %d\n", level);
return 0;
}
Show answer
Output: inside: 15 then outside: 5. The function changed its own copy, and the variable in main kept its value even though both are named level.
Program 3
#include <stdio.h>
#include <stdint.h>
void show(uint8_t v)
{
printf("%u\n", (unsigned)v);
}
int main(void)
{
show(260);
return 0;
}
Show answer
Output: 4. The value 260 does not fit in eight bits, and 260 minus 256 leaves 4.
Program 4
#include <stdio.h>
int rank(int score)
{
if (score >= 80) return 1;
if (score >= 50) return 2;
return 3;
}
int main(void)
{
printf("%d %d %d\n", rank(95), rank(50), rank(10));
return 0;
}
Show answer
Output: 1 2 3. Each call returns at the first matching condition. The arguments here are plain constants, so the evaluation order does not matter.
Program 5
#include <stdio.h>
void halt_msg(void);
int main(void)
{
halt_msg(1);
return 0;
}
void halt_msg(void)
{
printf("halted\n");
}
Show answer
Result: compile time error. The prototype says no parameters, so the call with one argument is rejected: too many arguments to function.
Program 6
#include <stdio.h>
void half(int n)
{
printf("%d\n", n / 2);
}
int main(void)
{
half(9.9);
half(-9);
return 0;
}
Show answer
Output: 4 then -4. The value 9.9 becomes 9 on conversion, and 9 divided by 2 is 4 with integer division. For -9, integer division truncates toward zero, giving -4.
Section C: Coding Challenges
Write each program on your own. Use prototypes, meaningful parameter names and compile with -Wall -Wextra.
| No | Task | Hint |
|---|---|---|
| 1 | Write a function that converts a temperature from Fahrenheit to Celsius and returns it as float | Subtract 32 and multiply by five ninths |
| 2 | Write a function clamp_range(value, low, high) that returns value limited to the range | Use early return statements |
| 3 | Write a function that returns 1 if a given year is a leap year and 0 otherwise | Combine divisibility checks with logical operators |
| 4 | Write a function that returns the number of digits in a positive integer | Divide by ten in a loop |
| 5 | Write a program that prints each command line argument with its position | Loop from 1 to argc minus 1 |
| 6 | Rewrite a call such as pair(next(), next()) so the order is guaranteed | Use two temporary variables |
Section D: Interview Questions With Model Answers
Basic Level
1. What is the difference between a function declaration and a definition?
A declaration tells the compiler the name, return type and parameter types. A definition also provides the body. A function can be declared many times but defined once in the program.
2. What are actual and formal arguments?
Actual arguments are the expressions in the call. Formal arguments are the parameter variables in the definition that receive copies of the actual values.
3. Does C support call by reference?
Not directly. C passes everything by value. You simulate reference behavior by passing a pointer, whose value is copied but which points to the caller’s data.
4. Can a function return more than one value?
A function returns one value, but that value can be a structure holding several fields, or the function can write results through pointer parameters.
5. What does the return statement do?
It ends the function immediately and sends a value back to the caller. In a void function, a bare return only exits early.
Intermediate Level
6. What is the difference between int f() and int f(void)?
In classic C, empty parentheses mean the parameters are unspecified and calls are unchecked, while void means no parameters and calls are checked. C23 treats empty parentheses like void.
7. Why should every function have a prototype?
It lets the compiler check argument count and types, convert compatible values and reject wrong calls. It also documents the interface in a header.
8. What happens if you call a function with fewer arguments than it expects?
The behavior is undefined. The missing parameters usually contain leftover data. With a prototype in scope the compiler stops the build.
9. Is the order of evaluation of function arguments defined in C?
No. All arguments are evaluated before the call, but in an unspecified order. If one argument modifies a variable that another reads, the behavior is undefined.
10. What does main return and to whom?
On a hosted system, an int to the operating system or calling process. Zero means success and any other value signals an error.
11. What is the difference between return and exit in main?
In main they have the same effect. exit can also be called from other functions, and it ends the entire program after cleanup.
Embedded Focused
12. Why is main usually an infinite loop in firmware?
No operating system exists to receive a status. If main returned, control would fall into startup code, which often hangs or resets, so the application loop must never end.
13. What runs before main on a microcontroller?
The reset handler from the startup code runs first. It sets the stack, copies initialized data from flash to RAM, clears the zero initialized area, may set up clocks and libraries, and then calls main.
14. Why avoid reading a hardware register twice in one argument list?
Evaluation order is unspecified, so reads that have side effects, such as a clear on read data register, may swap or repeat. Use separate statements.
15. Why prefer fixed width types like uint16_t in function signatures?
The size of int differs between targets. Fixed width types make behavior and overflow predictable across 8, 16 and 32 bit processors.
16. How can silent narrowing of arguments cause bugs?
Passing a wide value to a narrow parameter drops the high bits without warning in default builds. Clamp or validate values, and enable -Wconversion.
17. Which compiler flags help catch the issues in this chapter?
-Wall, -Wextra, -Wstrict-prototypes, -Wconversion, -Werror=implicit-function-declaration, and for runtime checks -fsanitize=undefined during testing on a host machine.
18. Why do coding standards like MISRA C forbid side effects inside function arguments?
They make evaluation order matter and cause results to change between compilers. Predictable code is easier to review, test and certify.
Common Mistakes Revisited
Skipping prototypes: The compiler loses its ability to protect you.
Expecting changes to reach the caller: Plain arguments are copies.
Packing side effects into one call: Split them into statements.
Letting firmware main return: Always end with an infinite loop.
Best Practices Checklist
- Prototype everything, and put shared prototypes in headers
- Use void for empty parameter lists
- Name parameters with units and meaning
- Keep arguments free of side effects
- Use int main(void) and return explicit statuses on hosted systems
- Compile with strict warnings from the first day
Summary and Key Takeaways
- Functions with arguments and return values are the backbone of C programs
- Declarations let the compiler check every call
- Argument evaluation order is unspecified, so write explicit statements
- main has a fixed name and signature, and firmware main never returns
Well done for completing this part of our free embedded C course. Continue with the next chapter of the free C programming course.
Frequently Asked Questions
Are these C interview questions suitable for freshers?
Yes. The basic level suits freshers, and the embedded section prepares you for firmware interviews.
Do I need hardware to practise this free C tutorial?
No. Every example runs on a normal computer with gcc, and the ideas transfer directly to microcontrollers.
How long should I spend on this quiz?
About an hour for a first attempt, including compiling the programs.
Which topic from this chapter is asked most in interviews?
Call by value, prototypes, the unspecified order of argument evaluation and the behavior of main are very frequent.
Is this free embedded C course enough to get a job?
It gives a strong base. Combine it with projects on a real board and practice with the questions on this page.
Keep Going With Free Embedded C
Move on to the next chapter of our free C programming course and keep building firmware skills.
