C Programming by Yash Sir: 7 Mistakes That Cost Marks and Their Fixes

Knowing C syntax is not enough if one unchecked operator or memory assumption changes the output. Learn seven common tracing mistakes and the reliable fix for each one.

KnowledgeGate Team

Exam prep & CS education

Updated 20 Nov 20265 min read
C Programming by Yash Sir: 7 Mistakes That Cost Marks and Their Fixes

You may know C syntax and still lose an entire output-tracing question because of one unchecked operator, boundary, side effect, or memory assumption. The difficult part is often not calculation, but knowing which rule controls it. These seven mistakes show the failure, the reason it happens, and the correction. If you need the full concept path first, begin with C Programming by Yash Sir: Worked Examples & Traces.

1. C operator precedence: stop reading strictly left to right

Students may assume the compiler evaluates whichever symbol appears first. In int x = 2 + 3 * 4;, multiplication has higher precedence: 3 * 4 = 12, then 2 + 12 = 14.

In int y = (2 + 3) * 4;, parentheses force addition first: 2 + 3 = 5, then 5 * 4 = 20.

Expression

First operation

Second operation

Result

2 + 3 * 4

3 * 4 = 12

2 + 12 = 14

14

(2 + 3) * 4

2 + 3 = 5

5 * 4 = 20

20

The results differ by 20 - 14 = 6. For full expression trees, typed subexpressions, and short-circuit traces, read C Operators and Expressions: Precedence and Worked Traces.

2. C integer division: casting after division is too late

The destination type does not decide how division happens. The operand types do.

  • int q = 17 / 5; uses integer division, so q = 3.

  • double r = 17 / 5; divides integers first, then stores r = 3.0.

  • double s = 17.0 / 5; uses floating-point division, giving s = 3.4.

double late = (double)(17 / 5); also gives 3.0: 17 / 5 = 3 happens before the cast. Convert an operand first with double fixed = (double)17 / 5; the division is now floating point and fixed = 3.4.

Write operand types beside the expression. A decimal destination cannot recover a fraction removed by integer division; only a floating-point operand changes the operation.

3. Increment operators: split side effects before tracing

Prefix and postfix are not interchangeable. Start with i = 4 and trace complete statements.

Statement

Value used

Value stored

i afterward

int a = i++;

4

a = 4

5

int b = ++i;

6

b = 6

6

Treating postfix as “increment, then use” wrongly predicts a = 5. In i++ + ++i, the changes to i are unsequenced, so the expression has undefined behaviour, not a portable numeric answer. Split side effects into statements and record each before-and-after value.

4. Assignment inside a condition: one character changes state and branch

In int marks = 25; if (marks = 0) puts("zero"); else puts("nonzero");, the condition assigns 0 to marks. Its value is false, so the program prints nonzero. That word does not mean marks remained 25; it is now 0.

Use marks == 0. With the same start, it is false, prints nonzero, and preserves marks = 25. Check = versus ==, with compiler warnings as a safety net.

The separate Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes sibling handles branch ordering, loops, switch, and break. The seven traces here handle expression semantics, storage, and strings.

5. Array loop bounds: a fifth iteration is not a fifth valid element

For int a[4] = {6, 8, 10, 12};, count n = 4, first index 0, and last index n - 1 = 3. Therefore, for (i = 0; i <= 4; i++) runs too far.

The legal additions give 6 + 8 + 10 + 12 = 36, but at i = 4, a[4] reads out of bounds. The final sum is undefined. Use i < 4; four iterations then finish with sum = 36. For array declarations, decay, and row-pointer types beyond this bound check, continue with Arrays and Pointers in C: 7 GATE Mistakes That Cost Marks.

6. C function arguments: a local swap does not change the caller

C swap diagram contrasts unchanged caller copies with pointer writes that exchange values 3 and 8.

C passes argument values to parameters. With caller values a = 3 and b = 8, swap(a, b) creates local x = 3 and y = 8. They become x = 8 and y = 3, but the caller remains unchanged.

The key question is not whether the local values swap, but which storage locations receive the writes. Copies isolate the caller; pointers direct the writes back to its objects.

With swap(&a, &b), p points to a and q to b. After temp = 3, *p = 8, and *q = 3, the caller has a = 8, b = 3. Draw caller and callee storage separately, then identify which object each write targets.

Two-panel C diagram compares call by value, where caller values remain 3 and 8, with pointer swap, where they become 8 and 3.

7. C strings: four visible characters still need a fifth byte

char word[4] = {'G', 'A', 'T', 'E'}; has no null terminator. With printf("%s", word), the search for \0 reads beyond the array, so no exact output is guaranteed.

Use char word[5] = "GATE";: index 0 = 'G', 1 = 'A', 2 = 'T', 3 = 'E', and 4 = '\0'. Now %s prints GATE. Capacity must equal at least visible characters plus one.

8. C output traces: classify the failure before calculating

A fast trace starts by classifying the operation before writing numbers. Use these three buckets:

  1. Defined expression: apply precedence and operand types, as in 17 / 5 = 3.

  2. Sequenced state change: record the value used and the value stored, as in i++ versus ++i.

  3. Invalid execution: stop and mark undefined behaviour when a read leaves bounds, a string lacks \0, or side effects are unsequenced.

That classification keeps a tempting numeric answer from hiding the real defect: a[4] is not a fifth element, i++ + ++i has no portable result, and swap(a, b) cannot change the caller's copies. For structured practice, use C Language Course: Concepts, MCQs & Coding.