C Programming Miscellaneous Formula Sheet: Macros, Memory, Types and File I/O

Revise mixed C programming questions by classifying the rule first, then checking exact macro, memory, file and operator traces.

KnowledgeGate Team

Exam prep & CS education

Updated 21 Nov 20266 min read
C Programming Miscellaneous Formula Sheet: Macros, Memory, Types and File I/O

A mixed C question can switch from a macro to an enum, heap allocation, a file mode or an operator trace without announcing the rule family. Classify the mechanism first, then write its rule, trigger, and exact macro, allocation, file or expression trace. Use the rules for last-mile revision after learning C from first principles. For the four-topic execution map with ownership, shadowing and checked stream returns, read Dynamic Memory Allocation, Macros, Scoping and File Handling in C. Enum values, translation, linking, runtime diagnostics and operator definedness add the remaining checks in the mixed set.

C miscellaneous revision: classify the mechanism before calculating

Question clue

Rule family

First action

#define, # or ##

Preprocessor

Expand tokens before evaluating

enum, struct, union or typedef

Named types

Identify the declaration and guaranteed facts

malloc, calloc, realloc or free

Heap lifecycle

Calculate requested bytes and track ownership

fopen, a mode string or EOF

Stream state

Trace open, check, transfer and close

==, <<, && or &

Precedence and definedness

Parenthesise before finding a value

Syntax or constraint violation

Translation

Inspect the diagnostic

Unresolved external symbol

Linking

Check definitions and linkage

Failure while executing

Run time

Trace the operation

Program runs but gives a wrong result

Logical error

Check the algorithm

If unfamiliar, use the C Programming Course as a concept reset.

The focused DMA, Macros, Scoping and File Handling in C: Formula Sheet with Worked Revision Notes uses a three-integer trace and keeps those four families together. The five-element allocation, enum facts, file states and diagnostic stages below use different worked values.

C preprocessor formula sheet: substitution, parentheses and token pasting

The path is source -> preprocessing -> compilation -> assembly -> linking -> executable. Preprocessing handles directives and tokens; compilation checks and translates C; assembly creates object code; linking resolves references.

Object-like macros replace names; function-like macros substitute arguments. # stringifies an argument, ## pastes tokens, conditional compilation selects source regions, and header inclusion supplies declarations. Inspect expanded tokens before applying precedence or type rules.

For #define SQR(x) (x*x), SQR(3+2) expands to (3+2*3+2). Multiplication happens first, so 3 + 6 + 2 = 11. With #define SQR(x) ((x)*(x)), it becomes ((3+2)*(3+2)), giving 5 * 5 = 25. Even this safer macro may evaluate an argument such as i++ more than once.

With #define JOIN(a,b) a##b and int var12 = 100;, JOIN(var,12) becomes var12, whose value is 100. C Preprocessor and Macros explains nested expansion, side effects and statement-macro traps. Substitution, parentheses and token pasting connect to the enum, memory, file and operator traces that follow.

Two-column macro diagram compares unsafe SQR expansion yielding 11 with parenthesised expansion yielding 25, plus JOIN(var,12) producing var12 with value 100.

C named types and storage facts: enums, structures, unions and typedef

Construct

What it creates

Guaranteed rule

When to use

enum

Named integer constants

First uninitialised enumerator is 0; explicit initialisers reset the sequence; each later uninitialised enumerator is the previous value plus 1

Trace constants

struct

Separate member storage

Alignment and padding may occur

Members coexist

union

Overlaid member storage

In ordinary use, one stored member value is active

Members share storage

typedef

Type alias

No object or distinct run-time type is created

Rename a type

For enum status { pass, fail, atkt };, the implicit values are pass = 0, fail = 1 and atkt = 2. Therefore stud1 = pass, stud2 = atkt, stud3 = fail prints 0, 2, 1 in that order.

Padding means structure size is not merely the sum of member widths. Union size likewise needs implementation size and alignment facts. The shown enum values follow from their declaration.

Dynamic memory allocation in C: byte formulas and ownership

Use requested bytes = element count * sizeof element. malloc(n * sizeof *p) requests uninitialised storage. calloc(n, sizeof *p) sets the allocated bytes to zero. For these int objects, all-bits-zero represents 0; it does not portably represent zero or null for every object type. realloc may resize and move an allocation; free releases it. Check allocation results and stay within the element count.

In a stated model where sizeof(int) = 4, calloc(5, sizeof *p) requests 5 * 4 = 20 bytes. After storing [6, 4, 9, 3, 8], the running sums are 6, 10, 19, 22, 30, so the final sum is 30.

To extend it, int *tmp = realloc(p, 8 * sizeof *p); requests 8 * 4 = 32 bytes. On success, assign p = tmp. Indexes 0 to 4 retain 6, 4, 9, 3, 8; initialise indexes 5 to 7 before reading. On failure, the original p remains valid. Finish with free(p); p = NULL;; this does not zero the released block. Dynamic Memory Allocation in C explains allocator ownership and failure handling beyond this five-element trace.

Heap allocation diagram shows calloc reserving 20 bytes for five integers, running sums reaching 30, realloc expanding to 32 bytes, and release leaving new slots indeterminate.

C file handling revision: modes, return values and one complete trace

Choose a mode from the required operations and preservation needs.

Mode

Meaning

r

Read existing

w

Write, create or truncate

a

Write at end, create if needed

r+

Read and write existing

w+

Read and write, create or truncate

a+

Read and append, with writes forced to the end

Follow declare FILE pointer -> fopen -> check against NULL -> transfer -> test the library return value -> fclose. Store fgetc in an int so EOF remains distinguishable from a character. Prefer while (fscanf(fp, "%d", &x) == 1) to an feof-driven loop.

If marks.txt contains 12 18 25 and is opened with r, successful reads produce (x, sum, count) states (12,12,1), (18,30,2), and (25,55,3). The next conversion is not 1, so the loop stops. With count > 0, (double)55 / 3 = 18.333...; %.2f prints 18.33. Then close the stream.

C operator and output rules: precedence, bits and definedness

For int x, y = 5, z = 5; x = y == z;, equality binds before assignment. The grouping is x = (y == z), the comparison yields 1, and x becomes 1.

For int a = 4, b = 2, c = 3;, a << b + c groups as a << (b + c). First b + c = 5, then 4 << 5 = 128. For an unsigned bit test, x = 44U is binary 101100, mask = 1U << 3 is 8, and x & mask = 8, so bit 3 is set.

Behaviour

Decision rule

Defined

The stated model permits one answer

Implementation-defined

Use the implementation's documented choice

Unspecified

One of several standard-allowed choices may occur

Undefined

The C standard provides no reliable behaviour or output prediction

i++ + ++i is undefined behaviour. Never manufacture a left-to-right trace for it.

How mixed C programming questions are tested: a 15-minute drill

Practise macro expansion, implicit enum values, allocation bytes, file modes and read loops, operator grouping, and error or undefined-behaviour classification.

Use this 15-minute cycle:

  1. Spend 3 minutes recreating the classification table.

  2. Spend 3 minutes expanding SQR(3+2).

  3. Spend 3 minutes rebuilding the 20-byte allocation trace.

  4. Spend 3 minutes reproducing the marks.txt states.

  5. Spend 3 minutes solving the equality and shift expressions.

After a missed day, resume with one cycle instead of doubling it. KnowledgeGate has about 20 questions in this miscellaneous multi-topic C area. For storage classes, implicit conversions and bitwise-only drills, read Miscellaneous C Programming: Storage Classes, Operators, Type Conversion, Macros and Bitwise, Fully Solved; the mixed traces here add enum values, heap and file states, and translation diagnostics. Use CS Fundamentals for Exams & Placements for adjacent subject notes.

C miscellaneous formula sheet: the short version and next step

Keep six checks: classify, expand macros, use guaranteed type and storage facts, calculate allocation bytes and ownership, trace stream returns, and parenthesise operators without guessing undefined behaviour.

Without looking back, reproduce four anchors: unsafe SQR(3+2) = 11, safe SQR(3+2) = 25, five 4-byte integers need 20 bytes and sum to 30 for the stated values, and the file average prints as 18.33. If you want concept review and practice together, continue with C Language Course: Concepts, MCQs & Coding.