Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations
Hello guys, welcome back to our blog. In this article, we will discuss the top 100 C coding-based “predict the output” interview questions with explanations. These questions are asked commonly in all automotive and IT interviews.
Ask questions if you have any electrical, electronics, or computer science doubts. You can also catch me on Instagram – CS Electrical & Electronics
- Top 100 Advanced Level ADAS Interview Questions With Answers
- Infineon Tops Global Microcontroller Market, Reinforces Automotive Semiconductor Leadership
- GPS vs IMU vs Sensor Fusion: The Complete Guide for Accurate Vehicle Localization
Coding-based “Predict The Output” Interview Questions
Question 1: Pointer & Increment
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%d %d %d", a, *p, *p++);
return 0;
}
Output:
10 10 10
Explanation:
- *p++ is equivalent to *(p++), so it returns the value at p before incrementing it.
- But since p is not used later, its increment doesn’t affect the rest.
- So: a = 10, *p = 10, *p++ = 10.
Question 2: Static Variable
#include <stdio.h>
void func() {
static int x = 0;
x++;
printf("%d ", x);
}
int main() {
func();
func();
func();
return 0;
}
Output:
1 2 3
Explanation:
- static int x retains its value across function calls.
- Each time func() is called, x is incremented and printed.
Question 3: String and Array
#include <stdio.h>
int main() {
char str[] = "Hello";
str[0] = 'Y';
printf("%s", str);
return 0;
}
Output:
Yello
Explanation:
- char str[] = “Hello” creates a modifiable array.
- You can change individual characters.
- str[0] = ‘Y’; changes “Hello” to “Yello”.
Question 4: Array Index
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
printf("%d", 2[arr]);
return 0;
}
Output:
30
Explanation:
- 2[arr] is same as arr[2]. It’s valid due to how pointer arithmetic works.
- So it prints the 3rd element → 30.
Question 5: Pre/Post-Increment
#include <stdio.h>
int main() {
int x = 5;
int y = x++ + ++x;
printf("%d %d", x, y);
return 0;
}
Output:
7 12
Explanation:
- x++ is 5 (post-increment), ++x becomes 7.
- So: y = 5 + 7 = 12, and finally x = 7.
Question 6: sizeof and Array
#include <stdio.h>
int main() {
int arr[10];
printf("%d", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Output:
10
Explanation:
- sizeof(arr) gives the total bytes.
- sizeof(arr[0]) gives bytes of one element.
- Their division gives the number of elements → 10.
Question 7: Null Terminator
#include <stdio.h>
int main() {
char str[5] = {'H','e','l','l','o'};
printf("%s", str);
return 0;
}
Ouput:
Hellox (or garbage after o)
Explanation:
- There’s no null character (\0) at the end.
- printf(“%s”, str); expects a null-terminated string.
- Output may include garbage after “Hello”.
Question 8: Recursion
#include <stdio.h>
int fun(int n) {
if(n == 0)
return 0;
return n + fun(n - 1);
}
int main() {
printf("%d", fun(5));
return 0;
}
Output:
15
Explanation:
- It’s the sum of the first 5 natural numbers: 5+4+3+2+1+0 = 15
Question 9: Pointer Confusion
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
int **q = &p;
**q = 20;
printf("%d", a);
return 0;
}
Output:
20
Explanation:
- *p refers to a. **q also refers to a.
- So **q = 20 means a = 20.
- **q: A double pointer is a pointer that stores the address of another pointer.
Question 10: Function Return and Comma Operator
#include <stdio.h>
int fun() {
return (1, 2, 3);
}
int main() {
printf("%d", fun());
return 0;
}
Output:
3
Explanation:
- The comma operator evaluates all expressions but returns the last one.
- So
fun()
returns3
.
Question 11: Const Pointer
#include <stdio.h>
int main() {
int x = 10, y = 20;
const int *p = &x;
//*p = 30; // Invalid
p = &y;
printf("%d", *p);
return 0;
}
Output:
20
Explanation:
- const int *p means the value at p is constant, but p itself can point to another variable.
- So you can do p = &y;, but not *p = something.
Question 12: Post Increment in Print
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d %d", x, x++, ++x);
return 0;
}
Output:
Undefined behavior
Explanation:
- Modifying a variable more than once without a sequence point (like in x++, ++x) leads to undefined behavior.
- The output may differ between compilers. Avoid in real-world code.
Question 13: Array Out of Bounds
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
printf("%d", arr[5]);
return 0;
}
Output:
Garbage value (or crash)
Explanation:
- Accessing arr[5] is out-of-bounds.
- It doesn’t always crash, but the result is undefined.
Question 14: String Literal Modification
#include <stdio.h>
int main() {
char *str = "Hello";
str[0] = 'Y';
printf("%s", str);
return 0;
}
Output:
Runtime Error / Segmentation Fault
Explanation:
- String literals are stored in read-only memory.
- Modifying them leads to crashes. Always use char str[] = “Hello” if you want to change it.
Question 15: Infinite Loop
#include <stdio.h>
int main() {
int i = 0;
while (i < 3)
printf("Hi\n");
return 0;
}
Output:
Hi
Hi
Hi
... (infinite)
Explanation:
- i is never incremented, so condition i < 3 always holds true.
- Infinite loop.
Question 16: Enum Basics
#include <stdio.h>
enum Days { MON, TUE = 5, WED, THU };
int main() {
printf("%d %d %d", MON, WED, THU);
return 0;
}
Output:
0 6 7
Explanation:
- MON = 0 by default
- TUE = 5, so WED = 6, THU = 7
Question 17: Void Pointer Arithmetic
#include <stdio.h>
int main() {
void *ptr;
int x = 10;
ptr = &x;
ptr++; // Invalid
return 0;
}
Output:
Compilation Error
Explanation:
- You cannot perform arithmetic on void pointers because the compiler doesn’t know the size of the object.
Question 18: main With Parameters
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%d", argc);
return 0;
}
Output:
1
Explanation:
- argc holds the count of arguments. The program name counts as one.
Question 19: Structure Memory Layout
#include <stdio.h>
struct test {
int x;
char y;
float z;
};
int main() {
struct test t;
printf("%lu", sizeof(t));
return 0;
}
Output:
12 (or 16 depending on padding)
Explanation:
- Padding is added to align memory (usually 4 or 8 bytes alignment).
- The actual size depends on the compiler/platform (use pragma pack() to control padding).
Question 20: Union Behavior
#include <stdio.h>
union data {
int i;
float f;
};
int main() {
union data d;
d.i = 10;
d.f = 3.14;
printf("%d", d.i);
return 0;
}
Output:
Garbage value
Explanation:
- In a union, all members share the same memory.
- Writing to f overwrites the memory of I.
Question 21: Dynamic Memory and Free
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int *)malloc(sizeof(int));
*p = 100;
free(p);
printf("%d", *p);
return 0;
}
Output:
Undefined (could be garbage or crash)
Explanation:
- Accessing memory after free() is undefined behavior.
- It might work, crash, or give garbage.
Question 22: Recursion with Static
#include <stdio.h>
void func() {
static int x = 5;
if (--x)
func();
printf("%d ", x);
}
int main() {
func();
return 0;
}
Output:
0 0 0 0
Explanation:
- x is static, initialized once.
- Each recursive call decrements it.
- After x == 0, prints 0 on return from each call.
Question 23: Bitwise Operators
#include <stdio.h>
int main() {
int a = 5;
int b = 9;
printf("%d", a & b);
return 0;
}
Output:
1
Explanation:
- 5 = 0101, 9 = 1001
- Bitwise AND: 0001 = 1
Question 24: Nested Structure
#include <stdio.h>
struct inner {
int x;
};
struct outer {
struct inner in;
};
int main() {
struct outer o;
o.in.x = 25;
printf("%d", o.in.x);
return 0;
}
Output:
25
Explanation:
- Shows nested structure access via outer.inner.member.
Question 25: Character Arithmetic
#include <stdio.h>
int main() {
char a = 'A';
a += 5;
printf("%c", a);
return 0;
}
Output:
F
Explanation:
- ‘A’ = 65, so 65 + 5 = 70 = ‘F’.
Question 26: Pointer to Array
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4};
int (*p)[4] = &arr;
printf("%d", (*p)[2]);
return 0;
}
Output:
3
Explanation:
- p is a pointer to an array of 4 integers.
- So (*p)[2] accesses the 3rd element.
Question 27: Ternary Confusion
#include <stdio.h>
int main() {
int a = 5, b = 10;
int c = a > b ? a : b > 15 ? b : 0;
printf("%d", c);
return 0;
}
Output:
0
Explanation:
- a > b is false → move to b > 15 ? b : 0
- b > 15 is also false → return 0
Question 28: Sizeof with Pointer
#include <stdio.h>
int main() {
int arr[10];
int *ptr = arr;
printf("%lu %lu", sizeof(arr), sizeof(ptr));
return 0;
}
Output:
40 8
Explanation:
- sizeof(arr) = 10 * 4 = 40
- sizeof(ptr) = size of pointer (usually 8 bytes on 64-bit system)
Question 29: Macro Expansion Surprise
#include <stdio.h>
#define SQUARE(x) x * x
int main() {
int a = 3;
printf("%d", SQUARE(a + 1));
return 0;
}
Output:
7
Explanation:
- Macro expands to: a + 1 * a + 1 → 3 + 1 * 3 + 1 → 3 + 3 + 1 = 7
- Should have been #define SQUARE(x) ((x)*(x))
Question 30: Const Puzzle
#include <stdio.h>
int main() {
const int x = 10;
int *p = (int*)&x;
*p = 20;
printf("%d", x);
return 0;
}
Output:
10 or 20 (undefined behavior)
Explanation:
- Modifying a const variable through a pointer is undefined behavior.
- Some compilers may show 10, others may reflect modified memory as 20.
Question 31: Volatile Trick
#include <stdio.h>
int main() {
volatile int x = 10;
int *p = (int *)&x;
*p = 20;
printf("%d", x);
return 0;
}
Output:
20
Explanation:
- Volatile tells the compiler not to optimize access.
- But if x is changed via a pointer, that change reflects because it’s still the same memory.
Question 32: Infinite Recursion Without Base Case
#include <stdio.h>
void foo() {
printf("Hi ");
foo();
}
int main() {
foo();
return 0;
}
Output:
Hi Hi Hi... (stack overflow eventually)
Explanation:
- Infinite recursion without stopping condition → leads to stack overflow crash.
Question 33: String and Character
#include <stdio.h>
int main() {
char *s = "A";
printf("%d", sizeof(s));
return 0;
}
Output:
8
Explanation:
s
is a pointer to a string →sizeof(s)
gives the size of the pointer, not the string length.
Question 34: Array vs Pointer Confusion
#include <stdio.h>
int main() {
int a[] = {1, 2, 3};
int *p = a;
printf("%d", *(p + 2));
return 0;
}
Output:
3
Explanation:
- Pointer arithmetic: *(p + 2) accesses the third element.
Question 35: Nested Ternary
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int c = 15;
int result = a > b ? a : b > c ? b : c;
printf("%d", result);
return 0;
}
Output:
15
Explanation:
- a > b is false → move to b > c ? b : c
- b > c is false → result is c = 15
Question 36: Assignment in Condition
#include <stdio.h>
int main() {
int x = 0;
if (x = 5)
printf("True");
else
printf("False");
return 0;
}
Output:
True
Explanation:
- Mistakenly used = instead of ==
- x = 5 evaluates to 5 → treated as true
Question 37: Macro with Side Effects
#include <stdio.h>
#define INCREMENT(x) x+1
int main() {
int a = 5;
printf("%d", INCREMENT(a) * INCREMENT(a));
return 0;
}
Output:
36
Explanation:
Let’s break it down properly:
- INCREMENT(a) → a+1
- So INCREMENT(a) * INCREMENT(a) → (a+1)*(a+1) → 6 * 6 = 36 ✅
- But macro has no parentheses: a+1 * a+1 = a + (1 * a) + 1 = 5 + 5 + 1 = 11
- ✅ Final Output: 11
➡️ Fix macro with parentheses: #define INCREMENT(x) ((x)+1)
Question 38: Character Array With No Null-Terminator
#include <stdio.h>
int main() {
char str[4] = {'H', 'e', 'l', 'o'};
printf("%s", str);
return 0;
}
Output:
Garbage after "Helo" or crash
Explanation:
- No ‘\0’ → leads to undefined behavior while printing a string.
Question 39: Toggle Bit
#include <stdio.h>
int main() {
int x = 9; // 1001
x = x ^ (1 << 0); // Toggle LSB
printf("%d", x);
return 0;
}
Output:
8
Explanation:
- LSB toggled from 1 to 0 → 1000 → decimal 8
Question 40: Swap Using XOR
#include <stdio.h>
int main() {
int a = 5, b = 9;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);
return 0;
}
Output:
9 5
Explanation:
- Classic XOR swap trick
Question 41: Signed vs Unsign
#include <stdio.h>
int main() {
unsigned int a = 10;
int b = -42;
if (a > b)
printf("Yes");
else
printf("No");
return 0;
}
Output:
No
Explanation:
- b is converted to unsigned → becomes a large positive number due to 2’s complement
- Actually: 10 > 4294967254 → false
Question 42: Infinite Loop Without Braces
#include <stdio.h>
int main() {
int i = 0;
while (i < 3)
printf("Hi\n");
i++;
return 0;
}
Output:
Infinite Hi (or compilation warning)
Explanation:
- Only printf is inside loop, i++ is outside → infinite loop
Question 43: Char Signedness
#include <stdio.h>
int main() {
char c = 255;
printf("%d", c);
return 0;
}
Output:
-1 (on most systems)
Explanation:
- char is signed by default → 255 = -1 (two’s complement)
Question 44: Bitwise NOT on Unsigned
#include <stdio.h>
int main() {
unsigned int a = 0;
printf("%u", ~a);
return 0;
}
Output:
4294967295
Explanation:
- ~0 in 32-bit unsigned is all 1s → max value for unsigned int (2³² – 1)
Question 45: Function Return Trap
#include <stdio.h>
int* get() {
int x = 10;
return &x;
}
int main() {
int *ptr = get();
printf("%d", *ptr);
return 0;
}
Output:
Garbage value / Segfault
Explanation:
- Returning address of local variable → invalid memory
Question 46: Floating Point Comparison
#include <stdio.h>
int main() {
float a = 0.1;
if (a == 0.1)
printf("Equal");
else
printf("Not Equal");
return 0;
}
Output:
Not Equal
Explanation:
- 0.1 is double, a is float → precision loss during comparison
Question 47: Pointer to Function + Array
#include <stdio.h>
void fun1() { printf("A "); }
void fun2() { printf("B "); }
int main() {
void (*arr[2])() = {fun1, fun2};
for(int i = 0; i < 2; i++)
(*arr[i])();
return 0;
}
Output:
A B
Explanation:
- Array of function pointers, calling them like regular functions.
Question 48: Sizeof vs strlen
#include <stdio.h>
int main() {
char str[] = "ABC";
printf("%lu %lu", sizeof(str), strlen(str));
return 0;
}
Output:
4 3
Explanation:
- sizeof includes ‘\0’ in a string literal.
- strlen does not include the null character.
Question 49: Function Pointer Array with Logic
#include <stdio.h>
void add(int x, int y) { printf("Sum: %d\n", x + y); }
void mul(int x, int y) { printf("Product: %d\n", x * y); }
int main() {
void (*fp[2])(int, int) = {add, mul};
fp[1](3, 4); // mul
fp[0](3, 4); // add
return 0;
}
Output:
Product: 12
Sum: 7
Explanation:
- Arrays of function pointers behave like regular arrays, and can be indexed/called directly.
Question 50: Sizeof Void Pointer
#include <stdio.h>
int main() {
void *ptr;
printf("%lu", sizeof(ptr));
return 0;
}
Output:
8
Explanation:
- sizeof(void*) gives the size of pointer (not of what it points to), typically 8 bytes on 64-bit.
Hey, we will add the remaining 50 questions shortly.
This was about “Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations”. Thank you for reading.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs
Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations. Top 100 C Coding-based “Predict The Output” Interview Questions With Explanations