Open In App

C Operators and Functions Interview Questions

Last Updated : 01 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C syntax structures are the building blocks that define how a C program is written and executed. From the way a program starts with main(), to the use of statements, loops, conditionals, and functions, everything follows a strict syntax.

1. What are different storage class specifiers in C?

auto, register, static, extern

2. What are static functions? What is their use?

In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files. See this

3. How to write your own sizeof operator? 

c
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)

See Implement your own sizeof for more details.

4. Why is C called a mid-level programming language?

C is called a mid-level programming language because it combines features of both low-level and high-level languages.

  • Low Level Features include direct access to memory through pointers and pointer arithmetic.
  • High-Level features include functions and structured programming.
  • This balance makes C suitable for system programming (like operating systems and compilers) as well as application development.

5. Why does int a = 5 / 2; give 2 instead of 2.5?

  • Both 5 and 2 are integers, so C performs integer division.
  • The decimal part is discarded (truncated).

To get 2.5, use floating-point numbers: float a = 5.0 / 2;.

C
#include <stdio.h>

int main() {
    int a = 5 / 2;
    printf("Integer division: %d\n", a);  // Output: 2

    float b = 5.0 / 2;
    printf("Floating-point division: %.1f\n", b);  // Output: 2.5

    return 0;
}

6. What are preprocessor directives in C?

Preprocessor directives start with a # symbol and are processed before the actual compilation begins.

  • Main types of Preprocessor Directives are Macros, File Inclusion, Conditional Compilation and Other directives like #undef, #pragma, etc.
  • Mainly these directives are used to replace a given section of C code with another C code. For example, if we write "#define PI 3.14", then PI is replaced with 3.14 by the preprocessor.
Compilation-Process-in-C
Preprocessor Directives in C

For more information, refer to the article - Preprocessor Directives in C 

7. What are header files and their uses?

C language has numerous libraries which contain predefined functions to make programming easier.

  • Header files contain predefined standard library functions. All header files must have a ‘.h’ extension.
  • Header files contain function and variable declarations that the preprocessor includes in your code before compilation, allowing you to use predefined functions from libraries or other source files.
  • Preprocessor directives instruct the compiler that these files are needed to be processed before the compilation. 
  • There are two types of header files, that is User-defined header files and Pre-existing header files.

For example, if our code needs to take input from the user and print desired output to the screen then ‘stdio.h’ header file must be included in the program as #include <stdio.h>. This header file contains functions like scanf() and printf() which are used to take input from the user and print the content.

For more information, refer to the article - Header Files in C

8. What is the difference between macro and functions?

A macro is a name that is given to a block of C statements as a pre-processor directive.

  • Macros are pre-processed which means that all the macros would be preprocessed before the compilation of our program.
  • Functions are not preprocessed but compiled. Since functions are compiled, type checking is done which finds type issues at the compile time rather than run time errors.
  • In general it is recommended to use functions over macros.

For more information, refer to the article - Macro vs Functions

Macro:

C
#include <stdio.h>

#define SQUARE(x) ((x)*(x)) // Macro

int main() {
    int a = 4;
    printf("Square using macro: %d\n", SQUARE(a)); // Output: 16
    return 0;
}

Function:

C
#include <stdio.h>

int square(int x) { // Function
    return x * x;
}

int main() {
    int a = 4;
    printf("Square using function: %d\n", square(a)); // Output: 16
    return 0;
}

9. What is the use of static variables in C?

A static local variable retains its value between function calls. Unlike normal local variables (which are reinitialized every time a function is called), a static local variable is initialized only once and it persists until the end of the program. An uninitialized static variables have an initial value assigned to 0.

C
#include <stdio.h>

void counter() {
    static int count = 0; // initialized only once
    count++;
    printf("Count = %d\n", count);
}

int main() {
    counter(); // Count = 1
    counter(); // Count = 2
    counter(); // Count = 3
    return 0;
}

Output:

Count = 1
Count = 2
Count = 3

Here without static, count will initialized in every function call so, count would always print 1.

10. What is the difference between pass by value and pass by reference?

Call by value

Call by Reference

Values of the variable are passed while function calls.The address of a variable(location of variable) is passed while the function call.
Dummy variables copy the value of each variable in the function call.Dummy variables copy the address of actual variables.
Changes made to dummy variables in the called function have no effect on actual variables in the calling function.We can manipulate the actual variables using addresses.
A simple technique is used to pass the values of variables.The address values of variables must be stored in pointer variables.

For more information, refer to the article - Pass by Value and Call by Reference

Pass by Value:

C
#include <stdio.h>

void change(int x) {
    x = 20;
}

int main() {
    int a = 10;
    change(a);
    printf("Call by Value: %d\n", a); // Output: 10 (no change)
    return 0;
}

Pass by Reference:

C does not support pass by value directly as there are no references (like we have in C++, Java and Python). But we achieve pass by reference using pointers in C.

C
#include <stdio.h>

void change(int *x) {
    *x = 20;
}

int main() {
    int a = 10;
    change(&a);
    printf("Call by Reference: %d\n", a); // Output: 20 (value changed)
    return 0;
}

11. What is an auto keyword?

Every local variable of a function is known as an automatic variable in the C language. Auto is the default storage class for all the variables which are declared inside a function or a block. Auto variables can only be accessed within the block/function they have been declared. We can use them outside their scope with the help of pointers. By default auto keywords consist of a garbage value. 

For more information, refer to the article - Storage Classes in C

C
#include <stdio.h>

void test() {
    auto int a = 10; // explicitly using auto (optional)
    int b = 20;      // also auto by default

    printf("a = %d, b = %d\n", a, b);
}

int main() {
    test();
    // printf("%d", a); // Error: 'a' is not accessible here (out of scope)
    return 0;
}

12. Write a program to print "Hello-World" without using a semicolon.

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

// Driver code
int main()
{
    // This will print the hello-world
    // on the screen without giving any error
    if (printf("hello-world")) {
    }
    return 0;
}

13. What is an extern keyword?

The extern keyword in C is used to declare a variable or function that is defined in another file or later in the same file. It tells the compiler that the actual memory allocation or definition exists elsewhere. Commonly used in multi-file programs, extern helps share global variables or functions across files without redefining them. It does not allocate memory, only the definition does.

C
#include <stdio.h>

int a = 10;        // Definition (memory is allocated here)
extern int b;      // Declaration (defined later)

int b = 20;

int main() {
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

14. Explain format specifiers.

In C format specifiers are used to tell the compiler what type of data will be present in the variable during input using scanf() or output using print().

  • %c: Character format specifier used to display and scan character.
  • %d, %i: Signed Integer format specifier used to print or scan an integer value.
  • %f, %e, or %E: Floating-point format specifiers are used for printing or scanning float values.
  • %s: This format specifier is used for String printing.
  • %p: This format specifier is used for Address Printing.

For more information, refer to the article - Format Specifier in C

15. What is the difference between getc(), getchar(), getch() and getche().

  • getc(): The function reads a single character from an input stream and returns an integer value (typically the ASCII value of the character) if it succeeds. On failure, it returns the EOF.
  • getchar(): Unlike getc(), gechar() can read from standard input; it is equivalent to getc(stdin).
  • getch(): It is a nonstandard function and is present in 'conio.h' header file which is mostly used by MS-DOS compilers like Turbo C. 
  • getche(): It reads a single character from the keyboard and displays it immediately on the output screen without waiting for enter key.

For more information, refer to the article - Difference between getc(), getchar(), getch(), getche()

16. Can we declare a function inside another function in C? If not, how do we mimic that behavior?

  • No, C does not support nested functions (functions inside other functions).
  • But you can mimic the behavior using:

Function pointers or
Calling helper functions inside another function.

Why this matters: This limitation helps enforce cleaner structure, but it challenges beginners coming from languages like Python.

Mimicking nested behavior:

C
#include <stdio.h>

void helper(){
    printf("Helper Function\n");
}

void mainFunction(){
    helper();
}

void main() {
    mainFunction();
}

17. What is a syntax error vs a logical error in C?

Syntax Error: Mistake in code structure, caught by the compiler. e.g., missing semicolon, unmatched braces.

C
#include <stdio.h>

int main() {
    int a = 10
    printf("a = %d\n", a);
    return 0;
}

Logical Error: Code compiles but produces wrong result. Harder to find, requires testing/debugging. e.g., divide by zero.

C
#include <stdio.h>

int main() {
    int a = 10, b = 5;
    int result = a - b; // Suppose programmer meant a + b
    printf("Result = %d\n", result); // Output: 5 instead of 15
    return 0;
}

18. Why does char c = 65; print A?

  • C uses the ASCII system where 65 corresponds to the character 'A'.
  • Characters in C are internally stored as integer values.
  • So assigning 65 to a char gives you 'A'.
C
#include <stdio.h>

int main() {
    char c = 65; // 65 is ASCII code for 'A'
    printf("Char c = %c\n", c); // Output: A
    return 0;
}