C Programming Interview Questions and Answers: Comprehensive Guide

C Programming Interview Questions and Answers: Comprehensive Guide

C Programming Interview Questions and Answers

Introduction

Preparing for a C programming interview? Here are some of the most frequently asked C programming questions, complete with answers and explanations to help you ace your technical interview.

1. Difference Between Function Declarations

Question: Is there any difference in the following declarations?

int myfun(int arr[]);
int myfun(arr[20]);
  1. True
  2. False

Answer: Option 1
Explanation: Yes, we must specify the data type of the parameter when declaring a function.

2. Using extern for Variables Across Files

Question: Suppose a program is divided into three files f1, f2, and f3, and a variable is defined in f1 but used in f2 and f3. Would we need the extern declaration in f2 and f3?

  1. True
  2. False

Answer: Option 1
Explanation: To use a variable f1 in FILE2.C and FILE3.C, declare it as extern int f1; in those files.

3. Restricting Global Variable Access

Question: Can a global variable be available to only some functions while being restricted to others?

  1. True
  2. False

Answer: Option 2
Explanation: To restrict access, define it locally in main() and pass it to necessary functions.

4. Global Variable Declarations

Question: Can a global variable have multiple declarations but only one definition?

  1. True
  2. False

Answer: Option 1
Explanation: Yes, extern should be used in multiple declarations, but only one definition is allowed.

5. Function Declarations vs Definitions

Question: Can a function have multiple declarations but only one definition?

  1. True
  2. False

Answer: Option 1
Explanation: Yes, but all declarations must be identical.

6. Loop Execution in C

Question: How many times will the while loop execute if short int is 2 bytes wide?

#include<stdio.h>
int main() {
    int j = 1;
    while(j <= 255) {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}
  1. Infinite times
  2. 255 times
  3. 256 times
  4. 254 times

Answer: Option 2
Explanation: The while(j <= 255) loop runs exactly 255 times. The short int size does not affect loop execution.

7. Logical vs Bitwise Operators

Question: Which of the following is NOT a logical operator?

  1. &
  2. &&
  3. ||
  4. !

Answer: Option 1
Explanation: & is a bitwise AND operator, not a logical operator.

8. Mathematical Operator Precedence

Question: What is the correct order of mathematical operators in programming?

  1. Addition, Subtraction, Multiplication, Division
  2. Division, Multiplication, Addition, Subtraction
  3. Multiplication, Addition, Division, Subtraction
  4. Addition, Division, Modulus, Subtraction

Answer: Option 2
Explanation: The correct order follows BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction).

9. Data Types Allowed in Switch-Case

Question: Which of the following data types cannot be used in a switch-case statement?

  1. Character
  2. Integer
  3. Float
  4. Enum

Answer: Option 3
Explanation: switch-case works only with int, char, and enum, not float.

10. Identifying Errors in a Switch Statement

Question: Point out the error, if any, in the following C program.

#include<stdio.h>
int main() {
    int i = 1;
    switch(i) {
        printf("This is a C program.");
        case 1:
            printf("Case1");
            break;
        case 2:
            printf("Case2");
            break;
    }
    return 0;
}
  1. Error: No default specified
  2. Error: Invalid printf statement after switch statement
  3. No Error and prints “Case1”
  4. None of the above

Answer: Option 3
Explanation: switch(i) evaluates as switch(1), so case 1 executes, printing “Case1”. The first printf is ignored.


Conclusion

These questions cover fundamental C programming interview topics, including functions, global variables, operators, loops, and control structures. Mastering these concepts will enhance your preparation for technical interviews and coding assessments.

  • A diagram of memory layout explaining global vs local variables.
  • Flowchart of a switch-case execution process.
  • Example outputs for loops and conditionals to clarify their execution behavior.

Stay tuned for more C programming insights and practice questions!

C Programming Interview Questions and Answers