Why C Programming is the Deciding Factor in WB JECA
In WB JECA, C Programming questions are intentionally constructed with subtle compiler nuances, pointer arithmetic, and operator precedence traps that cannot be solved through rote memorization.
Here are 5 of the most repeated traps from previous WB JECA question papers, with complete dry-run memory traces.
Trap 1: sizeof() Operator on Array vs Pointer Parameter
Consider the following C snippet:
#include <stdio.h>
void func(int arr[]) {
printf("%zu ", sizeof(arr));
}
int main() {
int arr[10];
printf("%zu ", sizeof(arr));
func(arr);
return 0;
}
Question: What is the output on a 64-bit architecture?
A)
40 40B)
40 8(Correct)C)
10 10D)
8 8
Analysis & Dry Run:
In
main(),arris a true stack-allocated array of 10 integers. Sincesizeof(int) = 4,sizeof(arr) = 10 * 4 = 40 bytes.When
arris passed tofunc(int arr[]), it decays into a pointer to its first element:int *arr.On a modern 64-bit architecture, any pointer variable occupies 8 bytes.
Therefore,
func()prints8. Output: 40 8.
Trap 2: Modification of String Literals vs Character Arrays
char *p = "JECA";
char s[] = "JECA";
s[0] = 'M'; // Valid! Modifies stack memory
p[0] = 'M'; // SEGMENTATION FAULT at runtime!
char *p = "JECA";stores the string literal in the read-only text/data segment (rodata) of memory. Attempting to write to it triggers a memory protection fault.char s[] = "JECA";copies the characters into stack memory, where write operations are fully permitted.
Trap 3: Pointer Arithmetic and Scaling
If integer pointer ptr points to address 0x1000, what is ptr + 3?
$$\text{Address} = \text{Base} + (3 \times \text{sizeof}(*\text{ptr})) = 0x1000 + (3 \times 4) = 0x1000 + 12 = 0x100C$$
Always remember: Pointer arithmetic scales automatically by the sizeof(data_type)!
Trap 4: Recursive Function Return Evaluation
What is the return value of mystery(5)?
int mystery(int n) {
if (n <= 1) return 1;
return n + mystery(n - 2);
}
Stack Trace:
mystery(5) = 5 + mystery(3)mystery(3) = 3 + mystery(1)mystery(1) = 1(Base case)Evaluation: $5 + (3 + 1) = 9$.
Key Takeaway for Exam Day
Whenever you see pointer and array combinations like *(*arr + 1) or *ptr++, immediately write down the memory addresses and variable pointers on your scrap paper. Dry-running beats guessing every time!