C Programming Miscellaneous Mistakes: 10 Traps in Macros, Types, Memory and Files

Ten common C traps are diagnosed at the right stage, from macro expansion and declarations to allocation, streams and undefined expressions. Each mistake includes the failure mechanism and a concrete repair.

KnowledgeGate Team

Exam prep & CS education

Updated 23 Nov 20265 min read
C Programming Miscellaneous Mistakes: 10 Traps in Macros, Types, Memory and Files

Miscellaneous C questions switch from preprocessing to declarations, storage, stream returns or operators without warning. Marks disappear when you calculate under the wrong rule. Each trap below names its cause, failure mechanism and repair. Use CS Fundamentals for Exams & Placements when a clue exposes a wider weak area.

C miscellaneous mistakes 1 and 2: classify before calculating

  1. Mistake 1, treating miscellaneous as a fact bag. Why it happens: the last line resembles output arithmetic. What goes wrong: you calculate before spotting a macro, declaration, storage or stream clue. Do instead: classify it.

Clue

First action

#define, #, ##

Expand tokens

enum, struct, typedef

Resolve declarations

malloc, calloc, sizeof

State storage

fopen, fscanf, EOF

Trace returns

Operators only

Apply precedence; check definedness

  1. Mistake 2, calling every failure run-time. Why it happens: all bad programs enter one bucket. What goes wrong: undeclared missing in int y = missing + 1; requires a translation diagnostic. In an ordinary linked build, extern int total; int main(void) { return total; } without a definition fails while linking. From int x = 5;, if (x = 0) sets zero and takes the else branch, a logical error if comparison was intended. Do instead: identify the earliest failing stage.

C macro mistakes 3 and 4: evaluate the expansion

Two macro traces compare unsafe SCALE and MIN expansions with parenthesized and single-evaluation repairs.
  1. Mistake 3, reading a macro as a function. Why it happens: SCALE(4) looks like a call. What goes wrong: with #define SCALE(x) x + 1, 3 * SCALE(4) expands to 3 * 4 + 1, so 12 + 1 = 13, not 15. Do instead: define #define SCALE(x) ((x) + 1). The result becomes 3 * ((4) + 1) = 3 * 5 = 15.

  2. Mistake 4, assuming parentheses mean one evaluation. Why it happens: grouping looks safe. What goes wrong: let MIN(a,b) be ((a) < (b) ? (a) : (b)). From i = 4, MIN(i++, 10) tests 4 and leaves i = 5; the true arm yields 5 and leaves i = 6. Thus m = 5, i = 6. Do instead: static inline int min_int(int a, int b) { return a < b ? a : b; } evaluates once. min_int(i++, 10) gives m = 4, i = 5. C Programming Miscellaneous Formula Sheet: Macros, Memory, Types and File I/O is the compact reference for rule triggers and values; these ten traps explain why tempting answers fail across mixed C rules.

C type mistakes 5 and 6: derive values and state the model

  1. Mistake 5, treating enum constants as positions from one. Why it happens: names look like ordered labels. What goes wrong: enum state { READY = 2, RUNNING, FAILED = 7, RETRY }; gives 2, 3, 7, 8, not 1, 2, 3, 4. Do instead: start at zero only when the first initializer is absent, add one after the previous value and restart after an explicit initializer.

  2. Mistake 6, adding member widths to predict structure size. Why it happens: padding is ignored. What goes wrong: when sizeof(char) = 1, sizeof(int) = 4 and int alignment is 4, struct Pair { char c; int n; }; occupies 8 bytes, not 5. c is at offset 0, offsets 1..3 are padding and n occupies 4..7. Do instead: use sizeof(struct Pair) on the stated implementation, never 8 as a universal guarantee.

C memory mistake 7: keep the allocated count

  1. Mistake 7. Why it happens: sizeof a / sizeof a[0] is copied onto a malloc pointer. What goes wrong: when sizeof(int *) = 8 and sizeof(int) = 4, n = 5; int *p = malloc(n * sizeof *p); requests 5 * 4 = 20 bytes for five integers. But sizeof p / sizeof p[0] is 8 / 4 = 2. A pointer carries no allocation count, so that ratio processes only two elements. Do instead: retain n = 5, check p != NULL, loop with i < n and pass the count with the pointer. C Dynamic Memory Allocation, Macros, Scope and File Handling: 10 Mistakes handles those four topics in depth; allocation here is one checkpoint within a ten-trap diagnostic.

C file-handling mistake 8: test the conversion count

  1. Mistake 8. Why it happens: EOF is memorised as the only stop. What goes wrong: let marks.txt contain 12 7 X 5. In while (fscanf(fp, "%d", &x) != EOF) sum += x;, the first two calls return 1, taking sum to 12, then 19. At X, fscanf returns 0, consumes no integer and leaves x = 7. The true condition repeats stale 7 at X, so no finite final sum exists. Do instead: use while (fscanf(fp, "%d", &x) == 1) sum += x;. It stops at 19; then handle malformed input. Miscellaneous C Programming: Storage Classes, Operators, Type Conversion, Macros and Bitwise, Fully Solved is the broader topic map; the input section isolates conversion-return failures.

C expression mistakes 9 and 10: group first, reject undefined traces

  1. Mistake 9, reading shifts and addition left to right. Why it happens: they seem equal in precedence. What goes wrong: with a = 4, b = 2, c = 3, a << b + c means a << (b + c). Calculate b + c = 5, then 4 << 5 = 128. The invented (4 << 2) + 3 = 19 is not the source grouping. Do instead: parenthesise by C precedence first.

  2. Mistake 10, forcing an undefined answer. Why it happens: you expect one tidy order. What goes wrong: from i = 4, i++ + ++i modifies i twice without sequencing, so neither its value nor final i is defined. Do instead: label it undefined. For a deliberate sequence: left = i gives 4; i++ makes 5; ++i makes 6; right = i gives 6; z = 4 + 6 = 10. This is new code, not the original's meaning.

Two-panel C expression diagram shows a defined shift-precedence trace yielding 128 beside an unsequenced increment expression marked as having no defined value and a separate sequenced replacement.

C miscellaneous mistakes: the 60-second audit and next step

Before submitting: classify the clue; find the earliest error stage; expand macros; count evaluations; derive enums and layout; carry allocation counts; test stream returns; apply precedence; reject undefined guesses.

Recheck the anchors: SCALE gives 13, repaired SCALE 15; enums give 2, 3, 7, 8; the stated structure is 8 bytes; pointer ratio 2 is not count 5; file sum is 19; shift result is 128; the unsequenced expression has no answer.

For practice, use C Language: Concepts, MCQs and Coding Questions for concepts, MCQs and coding.