interview question in c language

Top 10 C Language Interview Question Areas for 2026

·32 min read
Top 10 C Language Interview Question Areas for 2026

The short answer

The most common C interview questions cover pointers and memory management, strings and buffer overflows, function pointers, struct layout, macros, bitwise operations, static and scope, recursion, type casting and command-line arguments. Expect hands-on C coding questions too: reverse a string or a linked list, count set bits, check endianness, and spot bugs such as returning a pointer to a local variable.

  • Explain the difference between malloc and calloc, const pointers and pointers to const, and stack and heap without notes.
  • Know where C has undefined behaviour, for example modifying a string literal or i = i++ + 1.
  • Say why strncpy can leave a string unterminated, and how you would copy safely instead.
  • In coding questions, handle empty input and failed allocations, and free what you allocate.

Finding developers who can write robust, efficient C code can be a challenge. While many candidates can solve algorithmic puzzles, production-level C programming demands a deeper understanding of memory, security, and low-level system interactions. Memory safety bugs are a well-known source of security vulnerabilities in C and C++ code, which is why these skills need assessing. Generic problem-solving questions may not reveal a candidate's grasp of these core competencies.

These C interview questions work for both sides of the table: candidates preparing for a C programming interview and interviewers building one. Below you'll find the ten topic areas interviewers probe most, then a set of C coding interview questions with answers and quick-fire questions you can expect in a first round.

This guide moves beyond generic questions to provide a structured approach for evaluating practical skills. We'll explore 10 critical interview question in c language categories that help distinguish candidates who understand the language's complexities from those who only know the syntax. These questions are designed to simulate real-world challenges, probing a developer's knowledge of pointers, memory management, and security pitfalls.

To assess C language proficiency, interviews should move beyond rote algorithmic challenges and focus on applying C knowledge to real-world problems. For a broader perspective on assessing these applied skills, consider these top technical interview questions for engineers. The following categories provide a framework for targeted assessments that measure a candidate's readiness for C development. By focusing on areas like memory layout, function pointers, and bitwise operations, hiring teams can gather reliable data on a candidate's ability to build secure, high-performance systems.

1. Pointers and Memory Management

A candidate's grasp of pointers and memory management is fundamental for any serious C programming role. This isn't just about syntax; it's a core skill that separates developers who write robust, efficient applications from those who introduce bugs like memory leaks and segmentation faults. A solid interview question in C language in this area directly assesses a candidate's ability to manage system resources effectively.

Diagram illustrating stack and heap memory, with stack pointers referencing a malloc'd block on the heap, showing free().

The main concept revolves around the heap, a region of memory for dynamic allocation, and the stack, which handles automatic variables and function calls. Proficient candidates must demonstrate they can use functions like malloc and calloc to request memory from the heap and, crucially, use free to return it, preventing leaks that can degrade or crash an application over time.

How to Evaluate Candidate Proficiency

Start with a practical coding challenge that combines allocation, usage, and deallocation. This reveals their entire workflow and attention to detail.

Example Task: Ask the candidate to write a function that dynamically allocates an array of n integers. The function should initialize each element to its index, then return the pointer. You must also ask them to write a corresponding function to free the allocated memory.

A strong solution looks like this:

#include <stdio.h>
#include <stdlib.h>

int* create_and_init_array(int size) {
    if (size <= 0) {
        return NULL; // Handle invalid size
    }

    // Allocate memory on the heap
    int* arr = (int*)malloc(size * sizeof(int));
    if (arr == NULL) {
        // Allocation failed; handle the error gracefully
        perror("Failed to allocate memory");
        return NULL;
    }

    // Initialize the array
    for (int i = 0; i < size; i++) {
        arr[i] = i;
    }

    return arr;
}

void destroy_array(int** arr_ptr) {
    if (arr_ptr != NULL && *arr_ptr != NULL) {
        free(*arr_ptr);
        *arr_ptr = NULL; // Prevent dangling pointer
    }
}

Deeper Assessment Tips

  • Error Handling: Did the candidate check if malloc returned NULL? This is a key indicator of a developer who writes defensive, production-ready code.
  • Dangling Pointers: In the destroy_array function, setting the pointer to NULL after freeing is a best practice to prevent accidental use of freed memory. Ask them why this is important.
  • Bounds Checking: Inquire how they would prevent buffer overflows if the array were to be used by other functions. Do they understand the risks of writing past the allocated block?
  • Trace the Memory: Ask them to draw the stack and heap and explain where arr (the pointer) and the actual integer data are stored. This tests their conceptual understanding.

2. String Handling and Buffer Overflow Vulnerabilities

Handling strings in C is a common source of security vulnerabilities. Unlike modern languages with built-in string types, C relies on null-terminated character arrays and manual memory management. This opens the door to buffer overflows, a class of bugs that can lead to crashes or arbitrary code execution. A focused interview question in C language here probes a candidate's commitment to writing secure and resilient code.

Illustration of a buffer[10] overflow with 'strncpy' causing an explosion due to 'unsafe' string copy.

The core issue stems from using unsafe functions like strcpy() or gets() that don't perform bounds checking. If the source string is larger than the destination buffer, these functions will write past the buffer's boundary, corrupting adjacent memory on the stack. A developer who understands these risks and defaults to safer alternatives like strncpy() or snprintf() is important for any team building secure applications.

How to Evaluate Candidate Proficiency

Present a practical security challenge. This encourages candidates to think defensively and demonstrate their knowledge of safe coding practices beyond just basic syntax.

Example Task: Ask the candidate to write a function that safely copies a source string into a fixed-size destination buffer. The function must prevent buffer overflows and ensure the destination is always null-terminated.

A strong solution demonstrates careful handling of size limits:

#include <stdio.h>
#include <string.h>

void safe_string_copy(char* dest, const char* src, size_t dest_size) {
    if (dest == NULL || src == NULL || dest_size == 0) {
        return; // Handle invalid arguments
    }

    // Use strncpy to copy up to dest_size - 1 characters
    strncpy(dest, src, dest_size - 1);

    // Explicitly null-terminate the destination buffer
    dest[dest_size - 1] = '\0';
}

Deeper Assessment Tips

  • Rationale for dest_size - 1: Ask why strncpy is called with dest_size - 1 instead of dest_size. The correct answer relates to leaving space for the null terminator.
  • The strncpy Nuance: A sharp candidate might point out that strncpy doesn't guarantee null-termination if the source string is too long. Did they add the manual dest[dest_size - 1] = '\0'; line? This shows a deep understanding of the function's behavior.
  • Identify Vulnerabilities: Show them a code snippet using gets() or sprintf() with user-controlled input and ask them to identify the vulnerability and suggest a fix. This tests their practical security mindset.
  • Format String Bugs: Ask if they know the danger of printf(user_input);. This reveals their knowledge of format string vulnerabilities, another common C security flaw.
For job seekers

Practise before the real interview

Answers come easier once you have done the work. Cohesyve runs AI job simulations of the real work and gives you a scored report on where you lost marks. 3-day free trial, cancel anytime.

3. Function Pointers and Callbacks

Function pointers are a feature in C that allows for creating dynamic and extensible systems. A candidate's ability to use them effectively demonstrates an understanding of the language beyond basic procedural programming. An interview question in C language focused on function pointers tests their grasp of syntax and their ability to design flexible software architectures, such as those using callbacks or plugins.

This concept revolves around storing the memory address of a function in a pointer variable. This pointer can then be used to invoke the function indirectly, passed to other functions, or stored in data structures. This is the cornerstone of implementing callbacks, where a function's execution is deferred until a specific event occurs, allowing for decoupled and modular code.

How to Evaluate Candidate Proficiency

A practical coding challenge is a good way to assess both syntactic knowledge and conceptual application. Ask the candidate to build a small system that uses function pointers to achieve dynamic behavior.

Example Task: Ask the candidate to implement a simplified event notification system. It should have a function to "register" a callback function and another to "trigger" the event, which executes the registered callback. Demonstrate its use with two different callback functions: one that prints a message and another that performs a simple calculation.

A strong solution would look like this:

#include <stdio.h>

// Define a function pointer type for our callback
typedef void (*event_callback_t)(int);

// Global variable to hold the registered callback
static event_callback_t registered_callback = NULL;

void register_event_handler(event_callback_t handler) {
    printf("Registering a new event handler.\n");
    registered_callback = handler;
}

void trigger_event(int data) {
    if (registered_callback != NULL) {
        printf("Event triggered with data: %d\n", data);
        registered_callback(data); // Invoke the callback
    } else {
        printf("No event handler is registered.\n");
    }
}

// Example callback 1: Prints a message
void message_handler(int value) {
    printf("--- Message Handler: Received value %d ---\n", value);
}

// Example callback 2: Prints the square of a value
void square_handler(int value) {
    printf("--- Square Handler: %d * %d = %d ---\n", value, value, value * value);
}

Deeper Assessment Tips

  • Syntax Explanation: Before they write code, ask the candidate to explain the syntax for declaring a function pointer (return_type (*pointer_name)(parameter_types)). Can they articulate what each part means?
  • qsort Comparator: A classic test is asking them to implement a custom comparator function for the standard library's qsort. This demonstrates a real-world use case for function pointers.
  • Typedef Usage: Did they use typedef to simplify the function pointer syntax? This is a sign of experience and a desire for code readability.
  • Safety Checks: Note whether they check if the function pointer is NULL before attempting to call it. This is a critical safety measure to prevent segmentation faults.
  • Practical Applications: Discuss where they have seen or used callbacks before. Answers like event loops, signal handlers (signal()), or asynchronous I/O show practical, systems-level experience.

4. Struct Packing, Alignment, and Memory Layout

An understanding of how compilers arrange data in memory is a hallmark of an experienced C programmer. Questions about structure packing and alignment probe a candidate's grasp of performance optimization, cross-platform compatibility, and low-level system interactions. An interview question in C language focused on this topic is excellent for identifying developers who can write efficient code for memory-constrained or performance-critical systems.

The core concept is that CPUs access memory more efficiently when data is aligned to word-size boundaries. To achieve this, compilers often insert unused bytes, called padding, between a struct's members. A candidate who understands this can manually order struct members to minimize padding, reducing memory footprint and potentially improving cache performance, which is useful in areas like network protocol implementation or embedded systems.

How to Evaluate Candidate Proficiency

Present a practical challenge that requires the candidate to predict and then optimize a struct's memory layout. This tests both their theoretical knowledge and their ability to apply it.

Example Task: Give the candidate the following struct definition. Ask them to calculate its size on a typical 64-bit system and then reorder its members to minimize its size. They should explain the reasoning behind the padding and their optimization.

A strong solution demonstrates clear logic:

#include <stdio.h>
#include <stddef.h> // For offsetof

// Inefficiently ordered struct
struct InefficientPacket {
    char protocol_version; // 1 byte
    // 7 bytes padding
    double payload;        // 8 bytes
    int sequence_number;   // 4 bytes
    // 4 bytes padding
};

// Optimized struct
struct EfficientPacket {
    double payload;        // 8 bytes
    int sequence_number;   // 4 bytes
    char protocol_version; // 1 byte
    // 3 bytes padding
};

int main() {
    printf("Size of InefficientPacket: %zu bytes\n", sizeof(struct InefficientPacket));
    printf("Size of EfficientPacket: %zu bytes\n", sizeof(struct EfficientPacket));
    return 0;
}
// Expected Output:
// Size of InefficientPacket: 24 bytes
// Size of EfficientPacket: 16 bytes

Deeper Assessment Tips

  • Explain the Why: Ask the candidate to explain why the compiler adds padding. A good answer will mention CPU architecture, memory access cycles, and the performance penalties of unaligned access.
  • Use the Tools: Can they use sizeof() and the offsetof() macro to programmatically verify their predictions about member alignment and total size? This shows practical debugging skills.
  • Platform Differences: Inquire how the struct's size might differ on a 32-bit system versus a 64-bit system. This tests their understanding that alignment is platform-dependent.
  • Practical Implications: Discuss scenarios where this knowledge is important, such as parsing network packets, reading binary file formats, or interfacing with hardware registers, where exact memory layout is non-negotiable.

5. Macro Pitfalls and Preprocessor Directives

Macros and the C preprocessor offer tools for code substitution and conditional compilation, but their misuse can lead to difficult bugs and unmaintainable code. An interview question in C language focused on this topic gauges a candidate's understanding of compile-time operations, potential side effects, and the discipline required to use these features safely. This knowledge is helpful for writing clean, predictable, and robust C code.

The C preprocessor is a text-substitution tool that runs before the actual compilation. A candidate must understand that macros are not functions; they are direct text replacements. This distinction is the source of many common pitfalls, including operator precedence issues, multiple evaluation of arguments with side effects, and variable name collisions. A strong developer knows when to use a macro and when an inline function or a constant is a safer alternative.

How to Evaluate Candidate Proficiency

Begin with a "spot the bug" challenge involving a poorly written macro. This quickly reveals if they've encountered these issues in real-world scenarios or only have textbook knowledge.

Example Task: Present the following macro and ask the candidate to identify potential issues, explain them, and provide a corrected version. Then, ask for an alternative implementation using an inline function.

Buggy Macro: #define MAX(a,b) a>b?a:b

A strong candidate will identify two critical flaws:

  1. Operator Precedence: If used in an expression like int x = 2 * MAX(3, 4);, it expands to int x = 2 * 3>4?3:4;, which evaluates incorrectly as (2 * 3) > 4 ? 3 : 4, resulting in x = 3.
  2. Side Effects: If called with an argument that has a side effect, like MAX(i++, j++), the increment operator might be evaluated more than once, leading to unexpected behavior.

A well-reasoned solution looks like this:

#include <stdio.h>

// Corrected macro with parentheses to handle precedence and side effects
// (Note: This uses a non-standard GCC extension for type safety.)
#define SAFE_MAX(a, b) ({ \
    __typeof__(a) _a = (a); \
    __typeof__(b) _b = (b); \
    _a > _b ? _a : _b; \
})

// A more standard and often preferred alternative using an inline function
static inline int max_inline(int a, int b) {
    return a > b ? a : b;
}

int main() {
    int i = 5, j = 10;
    // The macro still has issues if types are different, but it's safer.
    printf("SAFE_MAX result: %d\n", SAFE_MAX(i, j)); 
    printf("Inline result: %d\n", max_inline(i, j));
    return 0;
}

Deeper Assessment Tips

  • Manual Expansion: Ask the candidate to manually write out the code expansion for a complex expression involving the buggy macro. This tests their fundamental understanding of the preprocessor.
  • Alternatives: Discuss the pros and cons of macros versus inline functions. Do they mention type safety, debugging ease (or lack thereof), and code bloat?
  • Header Guards: Ask them to explain the purpose of header guards (#ifndef, #define, #endif) or #pragma once. This shows their knowledge of building larger, modular projects.
  • Stringification and Concatenation: Probe their knowledge of the # (stringifying) and ## (concatenation) preprocessor operators with a simple practical example.

6. Bitwise Operations and Bit Manipulation

A candidate's ability to perform bit-level manipulation is a good indicator of their understanding of C. This skill is valuable in embedded systems, network programming, and high-performance computing where efficiency and direct hardware control are important. A well-crafted interview question in C language on this topic reveals if a candidate can think beyond high-level abstractions and work directly with the data's underlying representation.

A diagram illustrating bitwise operations with a binary string and symbols for AND, OR, XOR, and shifts.

This area tests a developer's fluency with bitwise operators (&, |, ^, ~, <<, >>). Proficient candidates can use these operators to implement efficient algorithms, manage status flags within a single byte, or pack data tightly to conserve memory. This is about more than just syntax; it’s about a mindset geared towards optimization and resourcefulness.

How to Evaluate Candidate Proficiency

Begin with a common but revealing coding challenge that requires bitwise logic. The goal is to see if they can identify an efficient, non-obvious solution over a more conventional, slower one.

Example Task: Ask the candidate to write two functions. The first should check if an integer is a power of 2. The second should count the number of set bits (1s) in an integer's binary representation. Both must be implemented using only bitwise operations.

A strong solution demonstrates clever use of bitwise properties:

#include <stdbool.h>
#include <stdio.h>

// Checks if a number is a power of 2
bool is_power_of_two(unsigned int n) {
    // Powers of 2 have only one bit set.
    // n > 0 handles the edge case of 0.
    // (n & (n - 1)) == 0 clears the least significant bit;
    // if the result is 0, only one bit was set.
    return (n > 0) && ((n & (n - 1)) == 0);
}

// Counts the number of set bits (1s)
int count_set_bits(unsigned int n) {
    int count = 0;
    while (n > 0) {
        // This trick clears the least significant set bit
        n &= (n - 1);
        count++;
    }
    return count;
}

Deeper Assessment Tips

  • Explain the "Why": Ask the candidate to explain why n & (n - 1) works for both problems. Can they trace the binary operations for a number like 12 (1100) to demonstrate the logic?
  • Practical Scenarios: Present a scenario, such as managing permissions (Read, Write, Execute) using a single integer. Ask them to write functions to set, clear, and check a specific permission flag using bit masks.
  • Signed vs. Unsigned: Inquire about the potential issues or differences when performing a right shift (>>) on a signed integer versus an unsigned one. This tests their knowledge of implementation-defined behavior and arithmetic vs. logical shifts.
  • Performance Implications: Discuss why a bitwise solution for counting set bits is often more performant than iterating through each bit with a loop and a modulus operator. This connects their technical knowledge to real-world performance impact.

7. Static Variables, Scope, and Linkage

A candidate's understanding of the static keyword is a good indicator of their maturity as a C developer. This isn't just a syntax detail; it's a concept tied to scope, storage duration, and linkage that dictates how variables and functions behave across different parts of a program. A well-crafted interview question in C language about static reveals if a candidate can build modular and maintainable systems.

The static keyword's meaning changes with its context. Inside a function, it creates a variable with a lifetime that persists across multiple calls. At the file level (global scope), it limits a variable or function's visibility to that specific translation unit, providing a mechanism for encapsulation and hiding implementation details.

How to Evaluate Candidate Proficiency

Move beyond simple definitions and present a practical scenario that forces them to apply the concept. A multi-file example is perfect for testing their grasp of linkage and scope.

Example Task: Ask the candidate to create a simple counter module. The module should have a function increment_counter() that increases an internal count and a get_counter() function that returns its current value. The key requirement is that the counter variable itself must not be accessible from outside the module's .c file.

A strong solution demonstrates proper encapsulation using static:

counter.c

#include "counter.h"

// This variable is only visible within counter.c
static int g_counter = 0; 

void increment_counter(void) {
    g_counter++;
}

int get_counter(void) {
    return g_counter;
}

counter.h

#ifndef COUNTER_H
#define COUNTER_H

// Public function declarations
void increment_counter(void);
int get_counter(void);

#endif // COUNTER_H

Deeper Assessment Tips

  • Predict the Output: Give them a function with a static local variable and ask for the output after calling it three times. Can they explain why the value is preserved?
  • Encapsulation: Ask them what would happen if the static keyword were removed from g_counter. Do they understand this would "pollute" the global namespace and break encapsulation?
  • Linkage Errors: Describe a scenario where another file tries to use extern int g_counter; to access the counter. Ask them to explain the linker error that would occur and why static prevents this.
  • Thread Safety: Discuss the implications of g_counter in a multi-threaded environment. This opens a conversation about race conditions and the need for synchronization mechanisms when dealing with shared static state.

8. Recursion, Stack Overflow, and Tail Call Optimization

Recursion offers an elegant way to solve problems like traversing tree data structures. However, in C, it comes with a risk: stack overflow. Each recursive call consumes a new frame on the call stack, and with finite stack memory, deep recursion can crash an application. A robust interview question in C language on this topic probes a candidate's understanding of this trade-off.

This area reveals a developer's grasp of how function calls are managed at a low level. A candidate who understands the call stack can predict performance bottlenecks, identify potential crashes, and know when an iterative approach is a safer, more scalable alternative. It’s a key differentiator for roles in systems programming, embedded systems, or performance-critical applications.

How to Evaluate Candidate Proficiency

Challenge the candidate to demonstrate their understanding by converting a classic recursive algorithm into an iterative one. This tests not just their knowledge of recursion but also their ability to manage state manually using data structures like a stack.

Example Task: Ask the candidate to implement a recursive function for a pre-order traversal of a binary tree. Then, ask them to write an iterative version of the same function and discuss the trade-offs, specifically what happens when the tree depth is very large.

A strong solution for the iterative version might look like this:

#include <stdio.h>
#include <stdlib.h>

// Assume Node and Stack structures are defined elsewhere

void preorder_traversal_iterative(Node* root) {
    if (root == NULL) {
        return;
    }

    // Create a stack for iterative traversal
    Stack* stack = create_stack(100); // Assume a max depth
    push(stack, root);

    while (!is_empty(stack)) {
        Node* current = pop(stack);
        printf("%d ", current->data); // Process the node

        // Push right child first, so left is processed first
        if (current->right != NULL) {
            push(stack, current->right);
        }
        if (current->left != NULL) {
            push(stack, current->left);
        }
    }
    
    destroy_stack(stack);
}

Deeper Assessment Tips

  • Stack Overflow: Ask them to calculate the approximate maximum recursion depth possible given a specific stack size (e.g., 1MB) and the size of a single stack frame for their function.
  • Trade-offs: Can they articulate why the iterative version is generally safer for potentially deep data structures? They should mention memory control (heap vs. stack) and avoiding stack overflow exceptions.
  • Tail Call Optimization (TCO): Pose a question about TCO. Do they know what it is, that C compilers don't guarantee it, and how it can prevent stack growth in certain recursive functions?
  • Appropriate Use Cases: Present a scenario, like programming for a resource-constrained embedded device, and ask if recursion is a suitable choice. Their reasoning will show their practical judgment.

9. Type Casting and Type Safety

C's type system is both powerful and permissive. This flexibility allows for low-level memory manipulation, but it also opens the door to subtle bugs. A candidate's ability to navigate type casting safely is a good indicator of their discipline and understanding of how data is represented in memory. A well-designed interview question in C language on this topic reveals whether a developer writes code that is robust or merely compiles.

Understanding type safety goes beyond simply knowing the syntax for casting. It involves a comprehension of implicit conversions, integer promotion rules, and the potential pitfalls of mixing signed and unsigned types. Proficient candidates recognize that a cast is an instruction to the compiler to trust them, and they use this power judiciously rather than as a quick fix to silence compiler warnings.

How to Evaluate Candidate Proficiency

Begin with a code analysis task that forces the candidate to predict the outcome of several implicit and explicit conversions. This directly tests their knowledge of data representation and potential data loss.

Example Task: Present the candidate with the following code snippet and ask them to explain the value of c, i, and the result of the comparison, detailing why each conversion happens.

#include <stdio.h>

void analyze_types() {
    int x = 300;
    // Implicit conversion from int to char (potential overflow)
    char c = x; 

    float f = 1.8f;
    // Implicit conversion from float to int (truncation)
    int i = f; 

    unsigned int u = 5;
    int s = -10;

    printf("Value of c: %d\n", c); // May not be 300
    printf("Value of i: %d\n", i); // Will be 1

    // Implicit conversion during comparison (integer promotion)
    if (s < u) {
        printf("-10 is less than 5\n");
    } else {
        printf("-10 is NOT less than 5\n");
    }
}

A strong candidate will correctly identify that c will hold a value like 44 (300 % 256) due to overflow, i will be 1 because of truncation, and the comparison will unexpectedly evaluate to false because s is promoted to a large unsigned integer.

Deeper Assessment Tips

  • Explain the "Why": Ask the candidate to explain the underlying reasons for the results. Do they mention two's complement for the signed integer, truncation for the float-to-int conversion, and the usual arithmetic conversion rules for the comparison?
  • Safe Conversions: Challenge them to write a "safe" conversion function, for example, int_to_char, that checks for potential overflow and returns an error code if the value is out of the target type's range.
  • Void Pointers: Discuss scenarios where void* is necessary (e.g., generic data structures, callback functions). Ask how they would use a void* to pass a struct and then safely cast it back to its original type.
  • Signed vs. Unsigned: Pose a simple question: "What happens when you subtract an unsigned int with value 5 from an unsigned int with value 3?" A good candidate will know the result is a large positive number due to wraparound, not -2.

10. Command-line Argument Parsing and Program Arguments

Beyond core algorithms, a candidate's ability to build practical, user-friendly tools is a good indicator of their real-world value. Command-line argument parsing with argc and argv is fundamental for creating flexible and scriptable C applications. A well-designed interview question in C language on this topic assesses not just C syntax, but also a developer's grasp of software design and user experience.

The main function's int argc (argument count) and char *argv[] (argument vector) parameters are the gateway for user input. A proficient developer must demonstrate they can iterate through argv, identify flags (like -v or --verbose), parse arguments with values (like --output=filename), and handle positional inputs, all while gracefully managing errors from invalid user input.

How to Evaluate Candidate Proficiency

Present a practical tool-building scenario that requires parsing a mix of argument types. This forces them to think about structure, error handling, and usability from the start.

Example Task: Ask the candidate to write a program that simulates a file-processing tool. It should accept an optional verbosity flag (--verbose), a required output file (--output=<filename>), and at least one positional input file name. The program should print helpful usage instructions if arguments are incorrect.

A strong solution demonstrates clear logic and robust validation:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

void print_usage(const char* prog_name) {
    printf("Usage: %s [--verbose] --output=<filename> <input_file1> [...]\n", prog_name);
}

int main(int argc, char *argv[]) {
    if (argc < 3) {
        print_usage(argv[0]);
        return 1;
    }

    bool verbose = false;
    char* output_file = NULL;
    int first_input_idx = -1;

    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--verbose") == 0) {
            verbose = true;
        } else if (strncmp(argv[i], "--output=", 9) == 0) {
            output_file = argv[i] + 9;
        } else {
            if (first_input_idx == -1) {
                first_input_idx = i;
            }
        }
    }

    if (output_file == NULL || first_input_idx == -1) {
        print_usage(argv[0]);
        return 1;
    }

    // ... Logic to process files would go here ...
    printf("Verbose mode: %s\n", verbose ? "ON" : "OFF");
    printf("Output file: %s\n", output_file);
    printf("Input files start at index: %d\n", first_input_idx);
    
    return 0;
}

Deeper Assessment Tips

  • Robustness: Did they handle missing arguments? What if --output= is provided with no filename? A production-minded developer anticipates user errors.
  • Conventions: Ask about standard conventions like -- to signify the end of options, or the difference between short (-v) and long (--verbose) options.
  • Usability: Evaluate the quality of their print_usage message. Is it clear, concise, and helpful? This detail separates good developers from great ones.
  • Scalability: How would their parsing logic change if they had to support 20 different options? Do they mention libraries like getopt as a more scalable solution for complex cases? This tests their knowledge of the broader C ecosystem.

C Coding Interview Questions With Answers

Topic questions show what a candidate knows. Coding questions show whether they can use it. These ten come up again and again in C programming interviews, from graduate roles to embedded and systems jobs. Each has a model answer and what a strong candidate adds. Try them yourself before reading the code.

1. Reverse a string in place

Swap characters from both ends towards the middle. A strong candidate handles empty and one-character strings, uses size_t for lengths, and points out that calling this on a string literal is undefined behaviour because literals must not be modified.

#include <string.h>

void reverse(char *s) {
    size_t n = strlen(s);
    if (n < 2) return;
    for (size_t i = 0, j = n - 1; i < j; i++, j--) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }
}

2. Count the set bits in an integer

The classic answer clears the lowest set bit each time round the loop, so it runs once per set bit rather than once per bit. Good follow-ups: why the parameter is unsigned (right-shifting negative signed values is implementation-defined), and compiler built-ins such as __builtin_popcount in GCC and Clang.

unsigned count_bits(unsigned x) {
    unsigned count = 0;
    while (x) {
        x &= x - 1;   /* clears the lowest set bit */
        count++;
    }
    return count;
}

3. Check whether a number is a power of two

A power of two has exactly one bit set, so n & (n - 1) is zero. The candidate should exclude zero explicitly.

int is_power_of_two(unsigned n) {
    return n != 0 && (n & (n - 1)) == 0;
}

4. Detect the machine's endianness at run time

Store 1 in a multi-byte integer and look at its first byte through an unsigned char pointer, which C allows for inspecting any object's bytes. Strong answers explain why endianness matters for network protocols and file formats, and mention htonl and ntohl.

int is_little_endian(void) {
    unsigned int x = 1;
    return *(unsigned char *)&x == 1;
}

5. Reverse a singly linked list

Walk the list once, turning each next pointer round. Look for the three pointers (previous, current, next), correct handling of an empty list, and an explanation of why it runs in O(n) time and O(1) extra space. A recursive version is fine as a follow-up, with a note on stack depth for long lists.

struct node {
    int value;
    struct node *next;
};

struct node *reverse_list(struct node *head) {
    struct node *prev = NULL;
    while (head) {
        struct node *next = head->next;
        head->next = prev;
        prev = head;
        head = next;
    }
    return prev;
}

6. Why doesn't this swap function work?

C passes arguments by value, so swap(int a, int b) swaps copies and the caller's variables are unchanged. The fix is to pass pointers. This question quickly shows whether someone really understands pointers.

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
/* call as: swap(&x, &y); */

7. What does sizeof return for an array parameter?

An array passed to a function decays to a pointer to its first element, so sizeof inside the function gives the size of a pointer, not of the array. That's why C functions take the length as a separate argument.

#include <stdio.h>

void f(int arr[10]) {
    printf("%zu\n", sizeof arr);   /* size of int *, not 10 * sizeof(int) */
}

int main(void) {
    int a[10];
    printf("%zu\n", sizeof a);     /* 10 * sizeof(int) */
    f(a);
    return 0;
}

8. Find the bug: returning a pointer to a local variable

buf lives on the stack and stops existing when the function returns, so the caller gets a dangling pointer. Acceptable fixes: have the caller pass in a buffer and its size, allocate with malloc and document that the caller must free it, or use a static buffer (and explain why that isn't thread-safe).

char *greeting(void) {
    char buf[16] = "hello";
    return buf;   /* bug: buf is destroyed when the function returns */
}

9. Copy a string safely into a fixed-size buffer

Many candidates reach for strncpy, but if the source is longer than the count, strncpy does not write a terminating null character, and if it is shorter, it pads the rest with zeros. A good answer either terminates the buffer explicitly or uses snprintf, and checks for truncation.

#include <stdio.h>

/* returns 0 on success, -1 if the source was truncated */
int copy_string(char *dst, size_t size, const char *src) {
    int n = snprintf(dst, size, "%s", src);
    return (n < 0 || (size_t)n >= size) ? -1 : 0;
}

10. Allocate and free a two-dimensional array

Look for checks on every allocation, clean-up of anything already allocated when one call fails, and a matching free function. Strong candidates also offer the alternative of one contiguous block (malloc(rows * cols * sizeof(int)) with index r * cols + c), which is faster and easier to free.

#include <stdlib.h>

int **make_grid(size_t rows, size_t cols) {
    int **g = malloc(rows * sizeof *g);
    if (!g) return NULL;
    for (size_t r = 0; r < rows; r++) {
        g[r] = calloc(cols, sizeof **g);   /* calloc zeroes the memory */
        if (!g[r]) {
            while (r--) free(g[r]);
            free(g);
            return NULL;
        }
    }
    return g;
}

void free_grid(int **g, size_t rows) {
    for (size_t r = 0; r < rows; r++) free(g[r]);
    free(g);
}

Quick-Fire C Interview Questions

Short questions like these often open a C interview or a phone screen. Each needs a one- or two-sentence answer.

  • What is the difference between malloc and calloc? malloc(size) allocates uninitialised memory. calloc(num, size) allocates memory for an array of num objects and sets every byte to zero. Both return a null pointer on failure, and the memory must be released with free.
  • What is the difference between const int *p and int *const p? const int *p is a pointer to a constant int: you can move the pointer but not change the value through it. int *const p is a constant pointer to an int: you can change the value but not where the pointer points.
  • What does volatile mean? It tells the compiler the value can change outside the program's normal flow (hardware registers, signal handlers, memory shared with an interrupt), so every read and write must actually happen and must not be optimised away. It does not make code thread-safe.
  • What does static do inside a function and at file level? Inside a function, a static variable keeps its value between calls. At file level, static gives a variable or function internal linkage, so it can't be seen from other source files.
  • Is i = i++ + 1; valid C? It compiles, but its behaviour is undefined: i is modified twice without a sequence point between the modifications. The same applies to i++ + i++. Strong candidates explain why and rewrite it.
  • What is the difference between a struct and a union? A struct gives each member its own storage, so its size is at least the sum of its members (plus padding). A union's members share the same storage, so its size is that of its largest member, and only one member holds a meaningful value at a time.
  • What are dangling, wild and null pointers? A dangling pointer points to memory that has been freed or has gone out of scope. A wild pointer has never been initialised. A null pointer deliberately points nowhere and can be tested before use. Setting pointers to NULL after free helps avoid the first two.
  • When would you use a macro instead of a const variable or an inline function? Macros are handled by the preprocessor with no type checking and can evaluate arguments more than once, so prefer const and static inline functions. Macros still make sense for include guards, conditional compilation and a few code-generation tricks.
  • What is the difference between the stack and the heap? The stack holds function call frames and local variables, is managed automatically and is limited in size. The heap holds memory you allocate with malloc and friends, lives until you free it, and leaks if you don't.
  • How do you find a memory leak or buffer overflow in C? Tools such as Valgrind and the AddressSanitizer option in GCC and Clang (-fsanitize=address), compiler warnings turned up (-Wall -Wextra), code review of every allocation and free, and tests that run under those tools.

Hiring for a C or systems role? Our software engineer interview questions cover the design and collaboration side of the interview.

Comparison of 10 C Interview Topics

Topic Implementation complexity Resource requirements Expected outcomes Ideal use cases Key advantages
Pointers and Memory Management High — pointer arithmetic, allocation/deallocation knowledge Requires test harnesses, memory-debugging tools (valgrind), platform tests Demonstrates memory safety, leak-free code and robust error handling Embedded, systems programming, performance-critical C apps Reveals deep low-level expertise; critical for preventing runtime memory bugs
String Handling and Buffer Overflow Vulnerabilities Moderate–High — careful string APIs and edge-case handling Needs input fuzzing, platform-specific library knowledge, security test cases Shows secure input handling and reduced overflow vulnerabilities Security-sensitive code, firmware, user-input processing Directly ties to security; exposes attention to defensive coding
Function Pointers and Callbacks Moderate — complex syntax but clear patterns Requires test code for callbacks and event scenarios Validates dynamic behavior, extensibility, callback correctness Event-driven systems, plugin architectures, embedded callbacks Demonstrates flexible, maintainable designs and advanced C usage
Struct Packing, Alignment, and Memory Layout Moderate — platform-dependent reasoning and measurement Needs cross-platform sizeof/offsetof tests and compiler flags Reveals memory layout awareness and optimization potential Embedded systems, network protocols, databases Helps optimize memory use and ensure ABI compatibility
Macro Pitfalls and Preprocessor Directives Moderate — conceptual but error-prone practices Requires code examples and macro-expansion tracing Identifies maintainability issues and preprocessor misuse Legacy codebases, low-level optimizations, embedded code Exposes subtle bugs and highlights modern refactoring opportunities
Bitwise Operations and Bit Manipulation Moderate — logical but low-level operations Needs bit-tracing examples and unsigned vs signed tests Demonstrates efficient low-level algorithms and flag handling Firmware, drivers, graphics, performance-critical code Enables compact, fast solutions for hardware and encoding tasks
Static Variables, Scope, and Linkage Low–Moderate — conceptual linking and scope rules Multi-file builds and linking tests to verify behavior Shows proper encapsulation, linkage control, and initialization Library development, multi-file systems, thread-sensitive code Validates module encapsulation and prevents naming collisions
Recursion, Stack Overflow, and Tail Call Optimization Moderate — algorithmic reasoning and stack limits Requires stack profiling and iterative alternatives for tests Assesses recursion suitability, stack safety, and optimization Algorithmic code, tree/graph processing, constrained stacks Reveals understanding of execution model and resource limits
Type Casting and Type Safety Low–Moderate — rules-based but subtle pitfalls Needs conversion tests, overflow checks, and review scenarios Exposes unsafe conversions and potential precision loss Systems code, security-critical routines, API boundaries Encourages data integrity and prevents subtle runtime errors
Command-line Argument Parsing and Program Arguments Low — well-defined APIs and patterns Requires CLI tests and portability checks (getopt) Demonstrates robust parsing, error messages, and UX CLI tools, utilities, system administration tools Practical, immediately useful skill for tool development

From Question Banks to Verified Skills: A Better Way to Hire

Navigating C language interviews requires more than just a list of questions. While this article provides a roundup of critical topics—from the nuances of pointers to the application of bitwise operations—the goal isn't to find candidates who have memorized answers. The objective is to identify engineers who can apply these concepts to build robust, secure, and efficient systems.

We've explored how a single interview question in C language can unpack a candidate's understanding of struct packing, macro pitfalls, and recursion. Yet, relying solely on this approach has its limits. The internet is saturated with these questions and their solutions, making it difficult to distinguish genuine problem-solving ability from rote memorization. A candidate might explain how to prevent a buffer overflow but fail to spot a similar vulnerability in your actual codebase.

Moving Beyond Abstract Knowledge

The most effective hiring processes recognize that technical assessment is not one-size-fits-all. The challenges of writing low-latency firmware are different from those of developing a high-performance database kernel, even though both rely on C. The key is to contextualize your assessment.

Instead of asking generic questions, consider how these concepts manifest in your day-to-day engineering challenges.

  • For Embedded Systems: Don't just ask about struct alignment; present a candidate with a memory-constrained scenario and have them optimize a data structure to fit within a specific memory page.
  • For High-Frequency Trading: Instead of a textbook bit-manipulation puzzle, provide a simplified market data protocol and ask them to write code that efficiently extracts specific flags using bitwise operations.
  • For Operating Systems: Rather than a general question on function pointers, have them implement a basic callback mechanism for an asynchronous I/O operation.

This shift from abstract theory to practical, role-specific application is the difference between hiring someone who knows C and hiring someone who can solve your problems with C.

The Power of Role-Specific Skill Verification

The limitations of traditional question banks highlight a gap in technical recruiting: the disconnect between the interview process and the job itself. An effective interview should feel less like a quiz and more like a preview of the work the candidate will be doing. This approach not only provides a more accurate signal of a candidate’s capabilities but also creates a better experience for the candidate, giving them a taste of the interesting challenges your team is solving.

By tailoring your assessment, you move from collecting subjective interview feedback to gathering objective data on who can perform. This evidence-based approach streamlines the process, allowing you to quickly identify and engage with a shortlist of top performers. You spend less time debating interview notes and more time building your engineering team. The right interview question in c language isn't just about what a candidate knows; it's about what they can do with that knowledge for your specific needs.


Ready to replace generic question banks with skill assessments that mirror your team's real-world challenges? Discover how Cohesyve uses AI to create tailored, interactive C language assessments directly from your job descriptions, helping you hire proven performers faster. Visit Cohesyve to see how you can build a better interview process today.

For job seekers

Go into your next interview having practised the work

Cohesyve runs AI job simulations of the real work and gives you a scored report on where you lost marks. You see which parts of the job you are ready for before anyone asks.

3 days

free trial

Unlimited

tests on Pro

Full

report on every test

Any

real job posting

3-day free trial · Then Pro $20/mo · Cancel anytime

For hiring teams

Interviewing candidates rather than being interviewed? Cohesyve finds and contacts candidates from your job description, tests them on a simulation of the real work and ranks them.

Hire C programmers tested on real code →

Practise before it counts

3-day free trial

Start practising